Get length of a video using regex and ffmpeg

后端 未结 6 1384
太阳男子
太阳男子 2021-02-01 21:36

From the following ffmpeg -i output, how would I get the length (00:35)--

$ ffmpeg -i 1video.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from \'/Users/d         


        
相关标签:
6条回答
  • 2021-02-01 22:06

    You can use the shell

    $ ff=$(ffmpeg -i video.mp4 2>&1)
    $ d="${ff#*Duration: }"
    $ echo "${d%%,*}"
    
    0 讨论(0)
  • 2021-02-01 22:12

    Do you want to do this in a bare shell pipeline, or read the result in a calling program?

    /\s+Duration: ((\d\d):(\d\d):(\d\d)\.(\d+))/
    

    … is a PCRE that will split the result up (replace the \. with [;:.] if ffmpeg might output the duration in frames rather than fractional seconds). In a Unix pipeline:

    | grep Duration: | cut -f2- -d: | cut -f1 -d, | tr -d ' '
    

    There are of course a billion other ways to express this.

    0 讨论(0)
  • 2021-02-01 22:12
    Duration: (\d\d):(\d\d):(\d\d(\.\d\d)?)
    

    should work. Whatever your language's $1 is will be the hours, $2 will be the minutes, $3 will be the seconds, and $4 will be just the centiseconds if they are exist.

    0 讨论(0)
  • 2021-02-01 22:19

    This way you get the duration in seconds. I think this is more convenient.

    ffprobe -loglevel error -show_streams inputFile.mp3 | grep duration | cut -f2 -d=
    

    ffprobe comes with ffmpeg so you should have it.


    EDIT: For a more dedicated version you could use for example

    ffprobe -loglevel error -show_format -show_streams inputFile.extension -print_format json
    

    Instead of JSON you could also use e.g. CSV or XML. For more output options look here http://ffmpeg.org/ffprobe.html#Writers

    0 讨论(0)
  • 2021-02-01 22:26

    Format (container) duration:

    $ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
    30.024000
    

    Adding the -sexagesimal option will use the HOURS:MM:SS.MICROSECONDS time unit format:

    0:00:30.024000
    

    Duration of the first video stream:

    $ ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
    30.000000
    

    https://trac.ffmpeg.org/wiki/FFprobeTips#Duration

    0 讨论(0)
  • 2021-02-01 22:27

    for latest version :

    $cmd = "avconv -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,";
    $output = exec($cmd);
    echo "\n\n==OUTPUT===|".$output."|=====\n\n";
    

    Now $output will hold your duration of video input.mp4

    format will be: hh:mm:ss.ms

    e.g 00:02:17.25

    0 讨论(0)
提交回复
热议问题