【bat文件+ffmpeg】實現(xiàn)視頻批量分割、轉(zhuǎn)格式、合并

1.split.bat
echo off
setlocal enabledelayedexpansion
set input=input.mp4
set outputFront=P
set startTime=00:00:00
set /a timeInterval=30
set endTime=00:02:46
call:timeToSec %startTime% s
set /a startSec=%s%
call:timeToSec %endTime% s
set /a endSec=%s%
set /a count=(%endSec%-%startSec%)/%timeInterval%
set /a rest=(%endSec%-%startSec%)%%%timeInterval%
if %rest% neq 0 (
set /a count=%count%+1
)
echo startSec=%startSec%,endSec=%endSec%,rest=%rest%,count=%count%
echo.
for /l %%i in (1,1,%count%) do (
set /a new=!startSec!+%timeInterval%
echo !new!
echo.
set st=void
call:secToTime !startSec! st
if !new! leq !endSec! (
set ed=void
call:secToTime !new! ed
set /a startSec=!new!
echo if1_: !startSec!
) else (
set ed=void
call:secToTime !endSec! ed
set /a startSec=!endSec!
echo if2_: !startSec!
)
echo loop:?%%i,start:?!st!,end:?!ed!
echo.
echo.
rem ffmpeg -i input.mp4 -ss 00:00:00 -to 00:01:30 -c:a copy -c:v copy output.mp4
ffmpeg -i %input% -ss !st! -to !ed! -y -c:a copy -c:v copy "%outputFront%%%i.mp4"
)
pause
REM==========================
:timeToSec
setlocal
set tt=%~1
set /a tt1=%tt:~0,2%
set /a tt2=%tt:~3,2%
set /a tt3=%tt:~6,2%
set /a ss=%tt1%*3600+%tt2%*60+%tt3%
(endlocal
set %~2=%ss%
)
goto:eof
REM==========================
:secToTime
setlocal
set /a s=%~1
set /a s1=%s%/3600
set /a s2=(%s%/60)%%60
set /a s3=%s%%%60
set tt=%s1%:%s2%:%s3%
(endlocal
set %~2=%tt%
)
goto:eof
2.changeFormat.bat
echo off
set inputFront=P
set concatNum=6
set originFormat=.mp4
set outputFormat=.ts
set inputName=concat:
for /l %%i in (1,1,%concatNum%) do (
echo %inputFront%%%i%originFormat%,%inputFront%%%i%outputFormat%
echo.
echo.
rem ffmpeg -i input.mp4 -c:a copy -c:v copy output.ts
ffmpeg -i %inputFront%%%i%originFormat% -y -c:a copy -c:v copy %inputFront%%%i%outputFormat%
)
pause
3.concat.bat
echo off
setlocal enabledelayedexpansion
set inputFront=P
set concatNum=6
set format=.ts
set outputName=test.mp4
set inputName=concat:
for /l %%i in (1,1,%concatNum%) do (
if %%i == %concatNum% (
set inputName=!inputName!%inputFront%
set inputName=!inputName!%%i%format%
) else (
set inputName=!inputName!%inputFront%
set inputName=!inputName!%%i%format%^|
)
)
echo !inputName!
rem ffmpeg -i "concat:01.ts|02.ts|03.ts" -c copy out.mp4
ffmpeg -i !inputName! -y -c:a copy -c:v copy %outputName%
pause
4.split2.bat
@echo off & title
cd /d %~dp0
for %%a in (*.mp4) do (
?ffmpeg -i "%%~sa" -y -codec copy -map 0 -f segment -segment_time 30 -q:v 1 "P%%02d.mp4"
)
pause