Batch Script to Combine and Transcode Folder of Video Files

Microsoft Windows specific usage questions
Forum rules
Please post only Windows specific questions in this forum category. If you don't know where to post, please read the different forums' rules. Thanks.
mikerost
New Cone
New Cone
Posts: 2
Joined: 31 May 2017 04:03

Batch Script to Combine and Transcode Folder of Video Files

Postby mikerost » 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.

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
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.

gkln
New Cone
New Cone
Posts: 4
Joined: 14 Feb 2018 14:46

Re: Batch Script to Combine and Transcode Folder of Video Files

Postby gkln » 12 Jan 2020 19:33

Thank you for the script. It took me a time to understand that files in the root folder are ignored - except the existence of an output-file. However, an outputfile of reasonable length compared with the input files was produced - which I failed to produce directly with the dialogs of VLC and despite the instructions given in
https://www.vlc-forum.de/thread/1160-streamen-ohne-unterbrechung/?postID=4898#post4898

It is not the fault of this script but I take the occasion to mention that the results were rather poor: I tried to combine 5 iPhone "Live Photos" (of type .mov, 1.5 seconds each) to a small .mp4 video file. The very first of those 5 in sequence played rather well, but the next 4 were heavily distorted, so the whole mp4 cannot be used. I wonder if there are still some parameters in VLC / this script to adapt to fit to the task.


Return to “VLC media player for Windows Troubleshooting”

Who is online

Users browsing this forum: No registered users and 50 guests