我有一个 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
)