Last active
October 8, 2024 13:12
-
-
Save benjaminbojko/45a09caf588a800fce39421e8bf68fb3 to your computer and use it in GitHub Desktop.
Convert folders of PNG sequences to VP8 with alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal enabledelayedexpansion | |
rem Set default values | |
set "width=1920" | |
set "height=1080" | |
set "framerate=30" | |
set "bitrate=25000k" | |
set "inputdir=%CD%" | |
set "outputdir=%CD%" | |
set "codec=vp8" | |
set "threads=0" | |
set "cpu_used=0" | |
set "two_pass=false" | |
rem Parse command-line arguments | |
:parse_args | |
if "%~1"=="" goto :end_parse_args | |
if /i "%~1"=="-w" set "width=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-h" set "height=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-fr" set "framerate=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-fps" set "framerate=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-br" set "bitrate=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-bitrate" set "bitrate=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-input_dir" set "inputdir=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-output_dir" set "outputdir=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-threads" set "threads=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-cpu_used" set "cpu_used=%~2" & shift & shift & goto :parse_args | |
if /i "%~1"=="-two_pass" set "two_pass=true" & shift & goto :parse_args | |
shift | |
goto :parse_args | |
:end_parse_args | |
rem Convert input and output directories to absolute paths | |
for %%I in ("%inputdir%") do set "inputdir=%%~fI" | |
for %%O in ("%outputdir%") do set "outputdir=%%~fO" | |
rem Create output directory if it doesn't exist | |
if not exist "%outputdir%" mkdir "%outputdir%" | |
rem Get current date and time | |
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I | |
set datetime=%datetime:~0,8%_%datetime:~8,6% | |
rem Set the filter string for centered video with transparent letterboxing/pillarboxing | |
set "filter=scale=iw*min(%width%/iw\,%height%/ih):ih*min(%width%/iw\,%height%/ih):flags=lanczos,pad=%width%:%height%:(%width%-iw*min(%width%/iw\,%height%/ih))/2:(%height%-ih*min(%width%/iw\,%height%/ih))/2:0x00000000,format=yuva420p" | |
rem Process each subdirectory | |
for /d %%D in ("%inputdir%\*") do ( | |
echo Processing directory: %%D | |
pushd "%%D" | |
rem Create a temporary directory for numbered PNGs | |
set "tempdir=%temp%\pngseq_%%~nxD" | |
if exist "!tempdir!" rmdir /s /q "!tempdir!" | |
mkdir "!tempdir!" | |
rem Copy and rename PNG files to numbered sequence | |
set "counter=0" | |
for %%f in (*.png) do ( | |
set /a "counter+=1" | |
copy "%%f" "!tempdir!\!counter!.png" > nul | |
) | |
if !counter! gtr 0 ( | |
echo Found !counter! PNG files in %%~nxD | |
rem Construct output filename | |
set "outfile=%%~nxD_%datetime%_%width%x%height%_%framerate%fps_%codec%_%bitrate%.webm" | |
rem Construct FFmpeg command | |
set "ffmpeg_cmd=ffmpeg -framerate %framerate% -i "!tempdir!\%%d.png" -c:v libvpx -b:v %bitrate% -auto-alt-ref 0 -crf 10 -deadline best -cpu-used %cpu_used% -threads %threads% -vf "!filter!" -r %framerate% -an" | |
if "%two_pass%"=="true" ( | |
echo Performing two-pass encoding for %%~nxD | |
!ffmpeg_cmd! -pass 1 -f null NUL | |
!ffmpeg_cmd! -pass 2 "%outputdir%\!outfile!" | |
) else ( | |
!ffmpeg_cmd! "%outputdir%\!outfile!" | |
) | |
if !errorlevel! neq 0 ( | |
echo Error occurred during FFmpeg processing for %%~nxD | |
echo FFmpeg command: !ffmpeg_cmd! "%outputdir%\!outfile!" | |
) else ( | |
echo Successfully processed %%~nxD | |
) | |
echo Processed !counter! frames in %%~nxD. | |
rem Clean up temporary directory | |
rmdir /s /q "!tempdir!" | |
) else ( | |
echo No PNG files found in %%~nxD | |
) | |
popd | |
) | |
echo All directories processed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
.\encode_all.bat -w 2112 -h 960 -fps 24 -input_dir ".\input_dir_with_subdirs\" -output_dir ".\output"