Skip to content

Instantly share code, notes, and snippets.

@pulimento
Last active January 10, 2021 18:06
Show Gist options
  • Save pulimento/51c989573ef46b1dc10816e3692c8bf4 to your computer and use it in GitHub Desktop.
Save pulimento/51c989573ef46b1dc10816e3692c8bf4 to your computer and use it in GitHub Desktop.
Re-encode mobile-recorded videos to archive them at a lower bitrate. Using FFMPEG
@echo off
REM
REM First parameter: Working input (can be a file or a directory)
REM Second parameter: Extra action. Possible values: [ ROTATECCW ]
REM
SET INPUT=%1
SET CURRENTDIR=%cd%
SET EXTRA_ACTION=%2
REM If not provided, use the current directory
IF "%~1" == "" SET INPUT=.
for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" (
REM ============================== Input is a directory
echo Processing directory - %INPUT%
cd %INPUT%
for %%i in (*.mp4) do CALL :ENCODE %%i,"%%~ni_1.mp4"
) else if "%%A" neq "-" (
REM ============================== Input is a file. Supposing INPUT FILE is %1
echo Processing file - %INPUT%
IF "%~2" == "ROTATECCW" (
REM ============================== File mode, extra action ROTATECCW
echo Extra action - Rotate counter-clockwise
ffmpeg -i %INPUT% -c copy -metadata:s:v:0 rotate=0 %~n1_temp.mp4
CALL :ENCODE %~n1_temp.mp4,"%~p1%~n1_1.mp4"
DEL %~n1_temp.mp4
) ELSE (
REM ============================== File mode, no extra actions
echo No extra action provided, process file normally
CALL :ENCODE %INPUT%,"%~p1%~n1_1.mp4"
)
) else (
echo %1 doesn't exists, exiting...
exit /B 1
)
::Back to current dir and exit
echo Exiting...
cd %CURRENTDIR%
EXIT /B %ERRORLEVEL%
REM ----- FUNCTIONS
REM Encode function. First parameter: Video file input. Second parameter: Video file output.
:ENCODE
echo ---- START Encoding file %~1 to %~2
REM Can't make inline comments, so here they are:
REM How to deal with VFR files, see https://superuser.com/questions/908295/
REM Audio is passthru
REM General, video and audio metadata passthru
ffmpeg -i "%~1" ^
-c:v:0 libx264 -crf 25 ^
-vsync vfr ^
-c:a copy ^
-map_metadata 0 -map_metadata:s:v 0:s:v -map_metadata:s:a 0:s:a ^
"%~2"
echo ---- END encoding file
EXIT /B 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment