我有一個 MP3 列表。我希望在螢幕上和檔案中看到每行一個 MP3,並註明其持續時間。這是我正在使用的批次檔:
@echo off
cls
if exist MP3Duration.txt del MP3Duration.txt
for %%i in (*.mp3) do (
echo %%i
echo %%i >> MP3Duration.txt
ffmpeg -i "%%i" | find "Duration" >> MP3Duration.txt
)
這有幾個問題:輸出文件包含文件名但沒有持續時間,螢幕顯示包含大量 ffmpeg 文本,並且對於每個文件,都會出現錯誤:「必須指定至少一個輸出文件」。
答案1
ffmpeg
控制台輸出僅供參考,不應使用腳本進行解析。改用ffprobe
:
ffprobe -loglevel error -select_streams a -show_entries stream=duration -sexagesimal -of default=noprint_wrappers=1:nokey=1 "%%i" | TEE.BAT -a MP3Duration.txt
答案2
若要取得以下格式的媒體資訊:
1.mp3|00:33:11
2.mp3|00:34:28
3.mp4|00:04:18
4.mp4|00:02:18
使用以下程式碼:
@echo off
>output.txt (
for %%F in (*.mp3 *.mp4) 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"
REM echo !duration!.%%d seconds
echo %%a:%%b:%%c
endlocal
)
)
)
pause
這會將結果記錄到 output.txt 檔案中,該檔案可以在 Excel 等電子表格程式中打開,以便進行進一步處理。
答案3
rem this assumes ffmpeg is in c:\ffmpeg
if exist filelist.txt ( del /F filelist.txt )
if exist list.txt ( del /F list.txt )
copy nul list.txt >nul
rem change exts as needed for your audio files
if exist *.mkv ( dir/b *.mkv >>list.txt )
if exist *.fl? ( dir/b *.fl? >>list.txt )
if exist *.avi ( dir/b *.avi >>list.txt )
if exist *.mp4 ( dir/b *.mp4 >>list.txt )
if exist *.mov ( dir/b *.mov >>list.txt )
if exist *.swf ( dir/b *.swf >>list.txt )
sort <list.txt >filelist.txt
for /F "delims=;" %%F in ( filelist.txt ) do (
for %%F in (^"%%F^") do (
for /f "tokens=2-5 delims=:., " %%a in (
'c:\ffmpeg\ffmpeg -i "%%F" 2^>^&1 ^| find "Duration:"'
) do (
set /a full=%%a * 60 * 60 + %%b * 60 + %%c
)
)
echo Length of %%F is %%a:%%b:%%c or !full! Seconds
)