check duration of audio files on the command-line

后端 未结 14 919
没有蜡笔的小新
没有蜡笔的小新 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:42
    mp3info -p "%m:%02s\n" filename
    

    gives you the length of the specified file in mm:ss format (mm can be greater than 59). For just the total number of seconds in the file, you'd use:

    mp3info -p "%S\n" filename
    

    To get the total length of all the mp3 files in seconds, AWK can help:

    mp3info -p "%S\n" *.mp3 | awk 'BEGIN { s = 0 }; { s = s + $1 }; END { print s }'
    
    0 讨论(0)
  • 2021-02-05 00:46

    A solution based on mplayer from commandlinefu by syssyphus that works with both audio and video files:

    sudo apt-get install mplayer
    find -type f -name "*.mp3" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
    

    Get the total length of all video / audio in the current dir (and below) in H:m:s

    Change the *.mp3 to whatever you want to match (e.g., *.avi, *.wav), you can remove it altogether if you want to check all files.

    Example of output: 00:00:37

    0 讨论(0)
  • 2021-02-05 00:47

    In addition to cdosborn's answer, to calculate the length of all .mp3 files recursively in subfolders of current directory and show the total sum result in days:hours:minutes:seconds format:

    In zsh:

    afinfo **/*.mp3 | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'
    

    In bash or sh:

    find . -name "*.mp3" -exec afinfo {} \; | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'
    

    The result is like this (7 days, 5 hours, 6 minutes, 58 seconds):

    $ afinfo **/*.mp3 | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'
    7:05:06:58
    $
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-05 00:53

    (When you don't have afinfo at your disposal) I got it recursively for all my files

    # install mp3info if not yet installed with
    sudo apt-get install mp3info
    

    with the find command, put the total seconds to a csv file (go to the directory with your e.g. mp3 files first)

    find . -name "*.mp3" -exec mp3info {} -p "%S\r\n" >> totalSeconds.csv \;
    

    Then open it in e.g. in LibreOffice and sum it up at the bottom (to get the hours) with

    =SUM(A{start}:A{end})/60/60
    
    0 讨论(0)
  • 2021-02-05 00:58

    mediainfo can do this, but mediainfo is one of those useful tools that's so badly documented that you almost need to know how to use it in order to learn how to use it (happens a lot in the linux world).

    After hours of trials and reading high and low, I finally got it to generate a recursive comma-separated list of file names and their duration in milliseconds.

    cd to the starting directory and issue the following command:

    find "$(pwd)" -type f -print0 | xargs -0 -I {} mediainfo --Inform="General;%CompleteName%,%Duration%" {} > list.txt
    

    The results will be output to list.txt.

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