Skip to content

Instantly share code, notes, and snippets.

@fproperzi
Created December 2, 2023 20:42
Show Gist options
  • Save fproperzi/a74fab558dd0f621e80c14e96d3155d0 to your computer and use it in GitHub Desktop.
Save fproperzi/a74fab558dd0f621e80c14e96d3155d0 to your computer and use it in GitHub Desktop.
ffmpeg merge and transcode
@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 vlc_transcode_origin=vcodec=h264,venc=x264{qp=%h264_quality%},fps=24,scale=Auto,acodec=mp4a,ab=128,channels=2,samplerate=44100
SET vlc_transcode_youtube_hd=vcodec=h264,vb=2000,venc=x264{profile=baseline},scale=Auto,width=1280,height=720,acodec=mp3,ab=192,channels=2,samplerate=44100,scodec=none
for %%a in ("%cd%") do set ff_path="%%~fa\ffmpeg.exe"
set ff_transcode_copy=-c copy
set ff_transcode_youtube=-c:v libx264 -preset medium -b:v 1500k -maxrate 3000k -c:a aac -b:a 128k
set ff_transcode_youtube_hd=-c:v libx264 -preset slow -crf 18 -vf "scale=-2:1080" -r 30 -c:a aac -b:a 192k
set ff_transcode_nitro_720=-c:v libx264 -preset medium -b:v 2M -maxrate 2M -bufsize 1M -vf "scale=-1:720" -r 30 -c:a aac -b:a 128k
set ff_xstreaming=-movflags frag_keyframe+empty_moov
rem echo %ff_path%
::---------------------------------------------------------------
:: INTRO
::---------------------------------------------------------------
ECHO Questo script attraverserà le directory da cui viene eseguito (cartella principale). Se una directory contiene
ECHO file video, li combinerà e li transcoderà in un singolo file video salvato nella cartella principale.
ECHO Ripeterà questo processo per qualsiasi altra directory presente nella cartella principale.
ECHO Lo script combinerà tutti i file per nome, quindi assicurati che abbiano il giusto nome per avere la giusta sequenza temporale.
ECHO:
ECHO Estensioni video valide = %ext%
ECHO:
::---------------------------------------------------------------
:: PROMPT WHETHER TO CONTINUE WITH MERGE
::---------------------------------------------------------------
rem CHOICE /C YN /M "Press Y to continue, N to cancel"
rem IF %ERRORLEVEL% EQU 2 GOTO end
echo Scegli un opzione:
echo 1. Nessuna transcodifica
echo 2. Youtube SD
echo 3. Youtube HD
echo 4. Nitro 720p
echo 5. Esci
choice /C 12345 /N /M "Inserisci il numero corrispondente all'opzione desiderata: "
if errorlevel 5 (
GOTO end
) else if errorlevel 4 (
set transcode=%ff_transcode_nitro_720%
) else if errorlevel 3 (
set transcode=%ff_transcode_youtube_hd%
) else if errorlevel 2 (
set transcode=%ff_transcode_youtube%
) else if errorlevel 1 (
set transcode=%ff_transcode_copy%
)
::---------------------------------------------------------------
:: 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
)
IF EXIST "ff_list.txt" (
del /f ff_list.txt
)
rem for /f "delims=" %%a in ('dir %ext% /on /b /a-d ') do call set vids=%%vids%% "%%a"
for /f "delims=" %%a in ('dir %ext% /on /b /a-d ') do echo file '%%a' >> ff_list.txt
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
rem "%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
rem %ff_path% -hide_banner -loglevel info -f concat -safe 0 -i ff_list.txt -c copy "%output_file%"
%ff_path% -hide_banner -loglevel info -f concat -safe 0 -i ff_list.txt %transcode% "%output_file%"
ECHO Done!
:end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment