批量提取視頻幀率及分辨率

最近遇到一個需求,需要做一些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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章