最近遇到一个需求,需要做一些frame rate conversion相关的工作,首先分析需求,有哪些FRC类型?这里需要批量提取一批文件的帧率及分辨率。
1.帧率查看工具
遇到视频,很难不想到ffmpeg,ffmpeg提供了3个实用的可执行文件。
- ffmpeg:视频转码等处理
- ffplay:播放相关
- ffprobe:媒体信息查看
这里选用ffprobe来查看媒体信息,首先,使用ffprobe来提取视频帧率及分辨率信息。
查看媒体信息
ffprobe 'https://vod.300hu.com/4c1f7a6atransbjngwcloud1oss/229f4e1c189448989732040705/v.f30.mp4?dockingId=37a56445-7bb7-44c6-9cbf-116e60d465a1&storageSource=3'
查看帧率
ffprobe 'https://vod.300hu.com/4c1f7a6atransbjngwcloud1oss/229f4e1c189448989732040705/v.f30.mp4?dockingId=37a56445-7bb7-44c6-9cbf-116e60d465a1&storageSource=3' -v quiet -show_streams 2>&1 |grep avg_frame_rate |head -1
2.批量分析文件中的帧率
这里使用shell脚本来执行批量操作,首先将视频url保存到文件中,每行保存一个,再按行读入进行分析即可,具体看下述代码。
#!/bin/sh
# 使用while循环读取$1文本
outfile="/Users/lemonhe/Downloads/outfile"
while read -r line
do
#判断是否读取到的数据是空行
if [ -n $line ]; then
#提取fps, width, height
#ffprobe输出到标准错误,使用2>&1将标准错误重定向到标准输出
#head -1取第一行
#tr -cd "[0-9]"取输出结果中的数字
fps=`ffprobe $line -v quiet -show_streams 2>&1 |grep avg_frame_rate | head -1`
width=`ffprobe $line -v quiet -show_streams 2>&1 |grep coded_width | head -1 |tr -cd "[0-9]"`
height=`ffprobe $line -v quiet -show_streams 2>&1 |grep coded_width | head -1 |tr -cd "[0-9]"`
outcome="$line $fps $width x $height"
echo $outcome
else
echo "current line is null"
# x=$x,"'"$line"'"
fi
echo $outcome >> $outfile
done < $1 #将$1的内容输入到while read循环中
结果如下:
这里有几个点需要阐明下:
- shell脚本前加入#!/bin/sh表示当前脚本的用/bin/sh来解释执行;
- ffprobe输出到标准错误,需要使用重定向2>&1后才能使用grep等管道命令,这里参考[1];
- 逐行处理通过while read -r line读文件来实现,参考[2]。
参考:
[1] https://blog.csdn.net/zhaominpro/article/details/82630528?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
[2]https://blog.csdn.net/suofeng1234/article/details/51790110