You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
graph TD
A[发起 HTTP 请求] --> B[读取前 8 字节]
B --> C[解析 box size + type]
C --> D{是否为 moov?}
D -->|否| E[跳过当前 box]
D -->|是| F[进入 moov box]
E --> B
F --> G[查找 mvhd]
G --> H[解析时长信息]
H --> I[结束,无需读取更多数据]
publicasyncTask<long?>GetVideoDurationWithRetryAsync(stringurl,intmaxRetries=3,TimeSpantimeout=default){if(timeout==default)timeout=TimeSpan.FromSeconds(30);for(intattempt=1;attempt<=maxRetries;attempt++){try{usingvarcts=newCancellationTokenSource(timeout);returnawaitGetVideoDurationAsync(url,cts.Token);}catch(OperationCanceledException)when(attempt<maxRetries){_logger.LogWarning($"Timeout on attempt {attempt} for {url}, retrying...");awaitTask.Delay(TimeSpan.FromSeconds(Math.Pow(2,attempt)));// 指数退避}catch(Exceptionex)when(attempt<maxRetries){_logger.LogWarning(ex,$"Error on attempt {attempt} for {url}, retrying...");awaitTask.Delay(TimeSpan.FromSeconds(2));}}returnnull;}
3. 批量优化策略
publicasyncTask<Dictionary<int,long?>>BatchGetDurationsAsync(IEnumerable<VideoInfo>videos,intbatchSize=10){varresults=newConcurrentDictionary<int,long?>();varbatches=videos.Chunk(batchSize);foreach(varbatchinbatches){vartasks=batch.Select(async video =>{varduration=awaitGetVideoDurationWithRetryAsync(video.Url);results.TryAdd(video.Id,duration);});awaitTask.WhenAll(tasks);// 批次间隔,避免对 CDN 造成压力awaitTask.Delay(TimeSpan.FromMilliseconds(100));}returnnewDictionary<int,long?>(results);}
FFprobe 流式解析原理分析与优化方案
背景
当前需要高性能批量获取 MP4 视频的时长信息,了解 FFprobe 的流式解析原理有助于我们:
FFprobe 流式解析原理
1. MP4 文件结构
2. 流式解析流程
graph TD A[发起 HTTP 请求] --> B[读取前 8 字节] B --> C[解析 box size + type] C --> D{是否为 moov?} D -->|否| E[跳过当前 box] D -->|是| F[进入 moov box] E --> B F --> G[查找 mvhd] G --> H[解析时长信息] H --> I[结束,无需读取更多数据]3. 关键优势
C# 实现优化建议
1. 进程池管理
当前实现每次都创建新进程,可以优化为进程池:
2. 超时和重试机制
3. 批量优化策略
性能监控和指标
1. 关键指标
2. 监控实现
网络优化建议
1. CDN 友好的请求模式
2. 错误处理策略
下一步行动
测试计划