Batch Script to Combine and Transcode Folder of Video Files
Posted: 27 Sep 2018 22:28
Hello all. I ran into a situation where I wanted to take a sequence of video files, combine them, and transcode them to smaller h264. I ended up writing a batch script to do this. This ends up saving me a lot of time, so I thought I would post my script here in case anyone is in a similar situation. There are of course assumptions made in it regarding accepted formats and transcoding settings, but this should be pretty easily tweak-able.
To run, copy this code into notepad and save as something like convert.bat
The script will look inside subfolders and convert any video files in there.
Code: Select all
@ECHO OFF
setlocal enableextensions
::---------------------------------------------------------------
:: DEFINE SETTING
::---------------------------------------------------------------
SET h264_quality=26
SET ext=*.MOV *.MTS *.AVI *.MP4 *.MPG
SET vlc_path=C:\Program Files\VideoLAN\VLC\vlc.exe
SET cmd_transcode=vcodec=h264,venc=x264{qp=%h264_quality%},fps=30,scale=Auto,acodec=mp4a,ab=128,channels=2,samplerate=44100
::---------------------------------------------------------------
:: INTRO
::---------------------------------------------------------------
ECHO This script will loop through the directories where it is run from (root folder). If a directory contains
ECHO video files, it will combine and transcode them into a single video file, and output this in the root
ECHO folder. It repeats this for any other directories present in the root folder.
ECHO:
ECHO The script will combine all files alphanumerically, so be sure they are named correctly so they sort in
ECHO the right time sequence. The script assumes video is 30fps, and you want to output to h264, suitable for
ECHO hosting and streaming. h264 quality is set to "%h264_quality%".
ECHO:
ECHO Valid video extensions = %ext%
ECHO:
::---------------------------------------------------------------
:: PROMPT WHETHER TO CONTINUE WITH MERGE
::---------------------------------------------------------------
CHOICE /C YN /M "Press Y to continue, N to cancel"
IF %ERRORLEVEL% EQU 2 GOTO end
::---------------------------------------------------------------
:: CHECK DIRECTORIES
::---------------------------------------------------------------
for /D %%d in (*) do (
cd %%d
call :run_folder "%%d"
cd ..
)
GOTO :end
::---------------------------------------------------------------
:: run folder routine
::---------------------------------------------------------------
:run_folder
::::::::::::::::::::::::::::::::::::
:: BEGIN
::::::::::::::::::::::::::::::::::::
SET cur_folder=%~1
SET output_file=../%cur_folder%.mp4
ECHO Checking folder %cur_folder%...
::::::::::::::::::::::::::::::::::::
:: CHECK THERE ARE FILES TO MERGE
::::::::::::::::::::::::::::::::::::
SET count=0
for %%x in (%ext%) do set /a COUNT+=1
IF %count% == 0 (
ECHO No suitable videos files were found in this directory. Videos must end in %ext% extension.
EXIT /B
)
::::::::::::::::::::::::::::::::::::
:: CHECK THAT OUTPUT FILE DOESN'T ALREADY EXIST
::::::::::::::::::::::::::::::::::::
IF EXIST "%output_file%" (
ECHO Outout file already exists, so won't attempt a merge.
EXIT /B
)
SET vids=
for /f "delims=" %%a in ('dir %ext% /on /b /a-d ') do call set vids=%%vids%% "%%a"
ECHO %count% videos will be merged. %output_file% will be created. Now starting...
:: note: the vlc://quit will exit vlc after completing the transcode, otherwise batch file can't loop to next folder
"%vlc_path%" %cmd_params% -vvv %vids% vlc://quit --sout-keep --sout=#gather:transcode{%cmd_transcode%}:std{access=file,mux=mp4,dst="%output_file%"} --sout-all
ECHO Done!
::::::::::::::::::::::::::::::::::::
:: END FUNCTION
::::::::::::::::::::::::::::::::::::
EXIT /B
::---------------------------------------------------------------
:: END SCRIPT
::---------------------------------------------------------------
:end
endlocal
PAUSE
The script will look inside subfolders and convert any video files in there.