check duration of audio files on the command-line

后端 未结 14 925
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 00:13

I need to check the duration of a group of audio files. Is there a simple way to do this on the unix command-line?

> duration *

I have the a

14条回答
  •  孤独总比滥情好
    2021-02-05 00:50

    The raw duration in seconds can be obtained with a high degree of precision with the use of ffprobe of ffmpeg, as follows:

    ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "filename.mp3" 2>/dev/null
    

    The output, easy to use in further scripting, is formatted like this:

    193.656236
    

    Extending upon that, the following will measure the total duration in seconds of all .mp3 files in the current directory:

    LENGTH=0; for file in *.mp3; do if [ -f "$file" ]; then LENGTH="$LENGTH+$(ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)"; fi; done; echo "$LENGTH" | bc
    

    And to measure the total length of audio files of several extensions, another wildcard may be appended:

    LENGTH=0; for file in *.mp3 *.ogg; do if [ -f "$file" ]; then LENGTH="$LENGTH+$(ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)"; fi; done; echo "$LENGTH" | bc
    

提交回复
热议问题