FFMPEG - batch extracting media duration and writing to a text file

后端 未结 1 2023
无人共我
无人共我 2021-01-07 12:33

I have ten media files in a folder. I want to create a text file containing two columns - filename and its duration (seconds):

video1.mp4|300 seconds 
vide         


        
相关标签:
1条回答
  • 2021-01-07 12:50

    Enumerate the media files and parse Duration: line in the output of ffmpeg -i:

    @echo off
    >output.txt (
        for %%F in (*.mpg *.mp4 *.mkv *.avi *.m4a *.flac *.mp3 *.wav) do (
            for /f "tokens=2-5 delims=:., " %%a in (
                'ffmpeg -i "%%F" 2^>^&1 ^| find "Duration:"'
            ) do (
                set /p =%%~nxF^|<nul
                setlocal enableDelayedExpansion
                set /a "duration=1%%a*3600 + 1%%b*60 + 1%%c - 366100"
                echo !duration!.%%d seconds
                endlocal
            )
        )
    )
    pause
    

    With ffprobe (a part of ffmpeg package) the durations will have microsecond precision:

    @echo off
    >output.txt (
        for %%F in (*.mpg *.mp4 *.mkv *.avi *.m4a *.flac *.mp3 *.wav) do (
            for /f "tokens=2 delims==" %%a in (
                'ffprobe "%%F" -show_entries format^=duration -v quiet -of compact'
            ) do (
                echo %%~nxF^|%%a seconds
            )
        )
    )
    pause
    

    Alternatively you can use a much faster MediaInfo CLI to output duration in milliseconds:

    >output.txt "C:\Program Files (x86)\MediaInfo\MediaInfoCLI.exe" ^
        --output=General;%%FileName%%.%%FileExtension%%^|%%Duration%%\r\n ^
        *.mpg *.mp4 *.mkv *.avi *.m4a *.flac *.mp3 *.wav
    
    0 讨论(0)
提交回复
热议问题