bash: displaying filtered & dynamic output of ffmpeg

折月煮酒 提交于 2020-01-06 14:47:08

问题


After this question whose the answer had partially resolved my problem. I would like to have a selected result of ffmpeg. So, with this command:

ffmpeg -y -i "${M3U2}" -vcodec copy -acodec copy "${Directory}/${PROG}_${ID}.mkv" 2>&1 | egrep -e '^[[:blank:]]*(Duration|Output|frame)'

The result is:

Duration: 00:12:28.52, start: 0.100667, bitrate: 0 kb/s
Output #0, matroska, to '/home/path/file.mkv':

But in the result I am missing this dynamic line:

frame= 1834 fps=166 q=-1.0 Lsize=    7120kB time=00:01:13.36 bitrate= 795.0kbits/s

This line changes every second. How can I modify the command line to display this line? My program should read this line and display the "time" updating in-place. Thanks

solution:

ffmpeg -y -i "${M3U2}" -vcodec copy -acodec copy "${Directory}/${PROG}_${ID}.mkv" 2>&1 |
      { while read line
        do
          if $(echo "$line" | grep -q "Duration"); then
            echo "$line"
          fi
          if $(echo "$line" | grep -q "Output"); then
            echo "$line"
          fi
          if $(echo "$line" | grep -q "Stream #0:1 -> #0:1"); then
            break
          fi
        done;
        while read -d $'\x0D' line
       do
          if $(echo "$line" | grep -q "time="); then
            echo -en "\r$line"
          fi
       done; }

Thanks to ofrommel


回答1:


You need to parse the output with CR (carriage return) as a delimiter, because this is what ffmpeg uses for printing on the same line. First use another loop with the regular separator to iterate over the first lines to get "Duration" and "Output":

ffmpeg -y -i inputfile -vcodec copy -acodec copy outputfile 2>&1 |
{ while read line
  do
     if $(echo "$line" | grep -q "Duration"); then
        echo "$line"
     fi
     if $(echo "$line" | grep -q "Output"); then
        echo "$line"
     fi
     if $(echo "$line" | grep -q "Stream mapping"); then
        break
     fi
  done;
  while read -d $'\x0D' line
  do
     if $(echo "$line" | grep -q "time="); then
        echo "$line" | awk '{ printf "%s\r", $8 }'
     fi
  done; }


来源:https://stackoverflow.com/questions/24548159/bash-displaying-filtered-dynamic-output-of-ffmpeg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!