我有数百个 mp3 文件,长度都是随机的。
我想从每个文件的开头和结尾剪掉 10 秒,而不需要告诉 ffmpeg 每个文件的持续时间。
举个例子,您已经知道文件长度为 1 分钟。它会从 50 秒开始删除,并删除前 10 秒,但这对数百个随机长度的 mp3 文件没有用,因为它要求您知道文件长度为 1 分钟……
ffmpeg -i input.mp3 -ss 00:00:10 -t 00:00:50 -c copy output.mp3
我搜索了几个小时却没有找到解决方案。
我真的认为这是不可能的,但我还是在这里问一下以防万一:)
答案1
我成功做到了。目前我只添加了 mp3 和 mp4 文件,但它的概念已经存在,并且已经过测试,可以正常工作。虽然下面的方法确实有效,但它可以改进,一旦我解决了所有问题,我将把修订后的版本编辑到这篇文章中。
修剪开始和结束.bat
rem Make this command window miminize itself...
:: if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
cd /d %~dp0
cls
@echo off
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem
rem Trim X seconds from start and/or end of media file.
rem
rem Supported file types:
rem - 3gp, asf, avi, flv, gif, m4v, mkv, mov, mp4, mpeg, mpg, rm, rmvb, swf, vob, webm, wmv.
rem
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
title Trim Start and End of Media Files
color 0f
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cls
if not exist ..\*.3gp if not exist ..\*.aac if not exist ..\*.aiff if not exist ..\*.asf ^
if not exist ..\*.avi if not exist ..\*.flac if not exist ..\*.flv if not exist ..\*.m4a ^
if not exist ..\*.m4v if not exist ..\*.mkv if not exist ..\*.mov if not exist ..\*.mp3 ^
if not exist ..\*.mp4 if not exist ..\*.mpeg if not exist ..\*.mpg if not exist ..\*.ogg ^
if not exist ..\*.opus if not exist ..\*.ra if not exist ..\*.rm if not exist ..\*.rmvb ^
if not exist ..\*.swf if not exist ..\*.vob if not exist ..\*.wav if not exist ..\*.webm ^
if not exist ..\*.wma if not exist ..\*.wmv ^
goto no_audio_or_video
if exist ..\*.3gp if exist ..\*.aac if exist ..\*.aiff if exist ..\*.asf ^
if exist ..\*.avi if exist ..\*.flac if exist ..\*.flv if exist ..\*.m4a ^
if exist ..\*.m4v if exist ..\*.mkv if exist ..\*.mov if exist ..\*.mp3 ^
if exist ..\*.mp4 if exist ..\*.mpeg if exist ..\*.mpg if exist ..\*.ogg ^
if exist ..\*.opus if exist ..\*.ra if exist ..\*.rm if exist ..\*.rmvb ^
if exist ..\*.swf if exist ..\*.vob if exist ..\*.wav if exist ..\*.webm ^
if exist ..\*.wma if exist ..\*.wmv ^
goto continue
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:continue
rem Set the amount of seconds to trim from the start and end of media file...
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set GAP=%%A
cls
echo.
echo ------------------------------------------------------------
echo Trim Start and End of Media File(s)
echo ------------------------------------------------------------
echo.
echo The start and end figures must be typed in seconds.
echo.
echo Here are some examples of minutes, in seconds...
echo.
echo [2 Minutes = 120 Seconds] [3 Minutes = 180 Seconds]
echo [5 Minutes = 300 Seconds] [10 Minutes = 600 Seconds]
echo [30 Minutes = 1800 Seconds] [60 Minutes = 3600 Seconds]
echo.
echo ------------------------------------------------------------
echo If you are only trimming the end, put 0 as the start figure.
echo If you are only trimming the start, put 0 as the end figure.
echo ------------------------------------------------------------
echo.
set /p start_trim="%GAP% Please type how many seconds to cut off the START: "
echo.
set /p end_trim="%GAP% Please type how many seconds to cut off the END: "
echo.
echo Please wait...
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Set a randomly named folder to allow multiple batch files to run without mixing up files...
set rnd_temp=%RANDOM%%RANDOM%%RANDOM%
md %rnd_temp%
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Move video/audio file from one folder up, to current folder...
if exist "..\*.3gp" move /y "..\*.3gp" "%rnd_temp%" >nul 2>&1
if exist "..\*.aac" move /y "..\*.aac" "%rnd_temp%" >nul 2>&1
if exist "..\*.aiff" move /y "..\*.aiff" "%rnd_temp%" >nul 2>&1
if exist "..\*.asf" move /y "..\*.asf" "%rnd_temp%" >nul 2>&1
if exist "..\*.avi" move /y "..\*.avi" "%rnd_temp%" >nul 2>&1
if exist "..\*.flac" move /y "..\*.flac" "%rnd_temp%" >nul 2>&1
if exist "..\*.flv" move /y "..\*.flv" "%rnd_temp%" >nul 2>&1
if exist "..\*.m4a" move /y "..\*.m4a" "%rnd_temp%" >nul 2>&1
if exist "..\*.m4v" move /y "..\*.m4v" "%rnd_temp%" >nul 2>&1
if exist "..\*.mkv" move /y "..\*.mkv" "%rnd_temp%" >nul 2>&1
if exist "..\*.mov" move /y "..\*.mov" "%rnd_temp%" >nul 2>&1
if exist "..\*.mp3" move /y "..\*.mp3" "%rnd_temp%" >nul 2>&1
if exist "..\*.mp4" move /y "..\*.mp4" "%rnd_temp%" >nul 2>&1
if exist "..\*.mpeg" move /y "..\*.mpeg" "%rnd_temp%" >nul 2>&1
if exist "..\*.mpg" move /y "..\*.mpg" "%rnd_temp%" >nul 2>&1
if exist "..\*.ogg" move /y "..\*.ogg" "%rnd_temp%" >nul 2>&1
if exist "..\*.opus" move /y "..\*.opus" "%rnd_temp%" >nul 2>&1
if exist "..\*.ra" move /y "..\*.ra" "%rnd_temp%" >nul 2>&1
if exist "..\*.rm" move /y "..\*.rm" "%rnd_temp%" >nul 2>&1
if exist "..\*.rmvb" move /y "..\*.rmvb" "%rnd_temp%" >nul 2>&1
if exist "..\*.swf" move /y "..\*.swf" "%rnd_temp%" >nul 2>&1
if exist "..\*.vob" move /y "..\*.vob" "%rnd_temp%" >nul 2>&1
if exist "..\*.wav" move /y "..\*.wav" "%rnd_temp%" >nul 2>&1
if exist "..\*.webm" move /y "..\*.webm" "%rnd_temp%" >nul 2>&1
if exist "..\*.wma" move /y "..\*.wma" "%rnd_temp%" >nul 2>&1
if exist "..\*.wmv" move /y "..\*.wmv" "%rnd_temp%" >nul 2>&1
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Remove non-standard characters from file names in the "%rnd_temp%"
rem folder, because ffmpeg cannot handle those characters...
xcopy "SpecialChar\*.bat" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
cd /d %rnd_temp%
call rename.bat >nul 2>&1
cd /d ..
del "%rnd_temp%\*.bat"
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Create OLD and NEW folders for files to be moved to...
if not exist "..\OLD" md "..\OLD"
if not exist "..\NEW" md "..\NEW"
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem FILE PROCESSING STARTS HERE:
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cls
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem ----------------------------------------
rem ----------------------------------------
rem --------------- mp4 files --------------
rem ----------------------------------------
rem ----------------------------------------
rem Trim the start of mp4 files (one at a time) using seconds specified in %start_trim% and move old files to the OLD folder...
md "%rnd_temp%\new_mp4"
if exist %rnd_temp%\*.mp4 FOR /F "tokens=*" %%G IN ('dir /b %rnd_temp%\*.mp4') DO start /wait /min ffmpeg.exe -y -i "%rnd_temp%\%%G" -ss %start_trim% -c copy "%rnd_temp%\new_mp4\%%~nG.mp4" -hide_banner & move /y "%rnd_temp%\%%G" "..\OLD" >nul 2>&1
rem Move the new files out of new_mp4, to the root of %rnd_temp% (old files were already moved to the OLD folder)...
move /y "%rnd_temp%\new_mp4\*.mp4" "%rnd_temp%"
rem Remove the now empty "%rnd_temp%\new_mp4" folder...
rd "%rnd_temp%\new_mp4"
rem Start the process to trim the end of mp4 files...
goto trim_end_mp4
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Trim the end of mp4 files (one at a time) using seconds specified in %end_trim% and move old files to the OLD folder...
:trim_end_mp4
if not exist "%rnd_temp%\Trim_mp4.bat" xcopy "TrimStartEnd\Trim_mp4.bat" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
if not exist "%rnd_temp%\ffmpeg.exe" xcopy "ffmpeg.exe" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
if not exist "%rnd_temp%\ffprobe.exe" xcopy "ffprobe.exe" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
for %%i in (%rnd_temp%\*.mp4) do (
call "%rnd_temp%\Trim_mp4.bat" "%%i"
)
cls
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem ----------------------------------------
rem ----------------------------------------
rem --------------- mp3 files --------------
rem ----------------------------------------
rem ----------------------------------------
rem Trim the start of mp3 files (one at a time) using seconds specified in %start_trim% and move old files to the OLD folder...
md "%rnd_temp%\new_mp3"
if exist %rnd_temp%\*.mp3 FOR /F "tokens=*" %%G IN ('dir /b %rnd_temp%\*.mp3') DO start /wait /min ffmpeg.exe -y -i "%rnd_temp%\%%G" -ss %start_trim% -c copy "%rnd_temp%\new_mp3\%%~nG.mp3" -hide_banner & move /y "%rnd_temp%\%%G" "..\OLD" >nul 2>&1
rem Move the new files out of new_mp3, to the root of %rnd_temp% (old files were already moved to the OLD folder)...
move /y "%rnd_temp%\new_mp3\*.mp3" "%rnd_temp%"
rem Remove the now empty "%rnd_temp%\new_mp3" folder...
rd "%rnd_temp%\new_mp3"
rem Start the process to trim the end of mp3 files...
goto trim_end_mp3
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Trim the end of mp3 files (one at a time) using seconds specified in %end_trim% and move old files to the OLD folder...
:trim_end_mp3
if not exist "%rnd_temp%\Trim_mp3.bat" xcopy "TrimStartEnd\Trim_mp3.bat" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
if not exist "%rnd_temp%\ffmpeg.exe" xcopy "ffmpeg.exe" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
if not exist "%rnd_temp%\ffprobe.exe" xcopy "ffprobe.exe" "%rnd_temp%" /i /r /v /k /f /c /h /y >nul 2>&1
for %%i in (%rnd_temp%\*.mp3) do (
call "%rnd_temp%\Trim_mp3.bat" "%%i"
)
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Delete ffmpeg.exe, ffprobe.exe and trim bat files from "%rnd_temp%"
if exist "%rnd_temp%\ffmpeg.exe" del "%rnd_temp%\ffmpeg.exe"
if exist "%rnd_temp%\ffprobe.exe" del "%rnd_temp%\ffprobe.exe"
if exist "%rnd_temp%\Trim_*.bat" del "%rnd_temp%\Trim_*.bat"
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rem Remove the random folder...
rd "%rnd_temp%\" >nul 2>&1
goto trim_complete
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:trim_complete
cls
echo.
echo Process is complete.
echo.
echo %start_trim% seconds were cut off the start.
echo %end_trim% seconds were cut off the end.
echo.
echo Exiting...
timeout 15 >nul
goto finished
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:no_audio_or_video
cls
echo.
echo.
echo There are no audio or video files to trim.
echo.
echo Exiting...
timeout 8 >nul
goto finished
rem ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:finished
exit
这修剪开始结束文件夹包含Trim_mp3.bat,Trim_mp4.bat等等,但我只会在这里发布 Trim_mp3.bat 的命令:
TrimStartEnd\Trim_mp3.bat
@echo off
cls
rem Use ffprobe to get the duration...
for /f "tokens=*" %%a in ('ffprobe -show_format -i %1 ^| find "duration"') do set _duration=%%a
set _duration=%_duration:~9%
for /f "delims=. tokens=1*" %%b in ('echo %_duration%') do set /a "_durS=%%b"
for /f "delims=. tokens=2*" %%c in ('echo %_duration%') do set "_durMS=%%c"
cls
rem Trim the end by the amount specified in %end_trim% (specified in parent batch file)
set /a "_durS-=%end_trim%"
set "_newduration=%_durS%.%_durMS%"
set "_output=%~n1"
if not exist "%rnd_temp%\new_mp3" md "%rnd_temp%\new_mp3"
start /wait /min ffmpeg.exe -y -ss 0 -i %1 -t %_newduration% -c copy "%rnd_temp%\new_mp3\%_output%.mp3"
rem Move the new file "%_output%.mp3" (that now has start and end trimmed) from "%rnd_temp%\new_mp3" to the NEW folder...
move /y "%rnd_temp%\new_mp3\%_output%.mp3" "..\NEW" >nul 2>&1
rem Delete leftover mp3 file that only had the start trimmed...
if exist %1 del %1
rem Delete the (now empty) "%rnd_temp%\new_mp3" folder...
if exist "%rnd_temp%\new_mp3" rd "%rnd_temp%\new_mp3"
rem ---------------------------------------------------------------------------
rem Now this whole process will repeat until all mp3 files have been processed.
rem ---------------------------------------------------------------------------
文件和文件夹结构如下:
ROOT\ffmpeg.exe
ROOT\ffprobe.exe
ROOT\Trim Start and End.bat <--- parent batch file
ROOT\TrimStartEnd\Trim_mp3.bat <--- called by parent batch file
现在您可能想知道您的 mp3/mp4 等文件去了哪里 - 它们去了 ROOT 文件夹旁边。
这样做的好处是,您可以只修剪开始、只修剪结束,或者同时修剪两者。
这不是一个干净的解决方案,因为它将 ffmpeg 和 ffprobe 复制到工作文件夹,但我不知道如何解决这个问题。也许其他人知道,提示提示。
享受!