Skip to content

Instantly share code, notes, and snippets.

@gwarser
Last active May 31, 2016 19:53
Show Gist options
  • Save gwarser/2026802 to your computer and use it in GitHub Desktop.
Save gwarser/2026802 to your computer and use it in GitHub Desktop.
Encode any video file to x264(crf) + copy/opus/vorbis + .mkv by drag&drop using ffmpeg.You can use default values or tune crf value, (psy tuning mode), video filters, output width, audio codec and quality.
@echo off
rem coding:oem
cd /d %~dp0
title %~nx0
set ffmpeg=ffmpeg.exe
set ext=mkv
rem AVC: ASP = 2; import math; (math.log10(ASP) / math.log10(2)) * 6 + 12
rem ASP: AVC = 18; import math; 10**((AVC - 12) / 6 * math.log10(2))
echo ASP: 2; 2,25; 2,33; 2,5; 2,67; 2,75; 3,0; 3,56; 4;
echo AVC: 18; 19,01; 19,33; 19,9; 20,50; 20,76; 21,5; 23,00; 24;
set vbt=19,9
set /p vbt=Video crf (0-51.0)[%vbt%]:
set vbt=%vbt:,=.%
echo.
echo ...veryfast faster fast medium slow slower veryslow...
set preset=slow
set /p preset=Preset [%preset%]:
echo.
rem echo film animation grain stillimage psnr ssim fastdecode zerolatency
rem set tune=
rem set /p tune=Psy tuning [%tune%]:
rem if not ""=="%tune%" set tune=-tune %tune%
rem echo.
rem ffmpeg.exe -i %1 -vf select=eq(pict_type\,I)*(isnan(prev_selected_t)+gte(t-prev_selected_t\,10)),cropdetect -vsync passthrough -an -f rawvideo -y nul
rem def hqdn3d=4:3:6:4.5
rem w3fdif
echo null,crop=width:height:x:y,idet,yadif,hqdn3d=3:2:2:2,unsharp=7:7:0.5
set prefilter=null
set /p prefilter=Add filter (before resize)[%prefilter%]:
rem if not ""=="%prefilter%" set prefilter=%prefilter%,
echo.
set width=iw
set /p width=Output width (iw-input width)[%width%]:
echo.
:badaudio
set abt=v
set /p abt=Select audio encoder (c-copy, o-opus, v-vorbis, m-mp3)[%abt%]:
rem /i - case insensitive
if /i "%abt%"=="c" goto copy
if /i "%abt%"=="o" goto opus
if /i "%abt%"=="v" goto vorbis
if /i "%abt%"=="m" goto mp3
goto badaudio
:copy
set acodec=copy
goto audioend
:opus
set abt=
echo Opus: speech ^>= 32; music ^>= 64kb/s; recommended ^>= 96
echo Default: 64k * opus-^>stream_count + 32k * coupled_stream_count
set /p abt=Opus bitrate in kb/s (6..510)[%abt%]:
if "%abt%" EQU "" (
set acodec=libopus
goto audioend
)
if %abt% LSS 6 goto badaudio
if %abt% GTR 510 goto badaudio
set acodec=libopus -vbr on -b:a %abt%k
goto audioend
:vorbis
set abt=
echo Ogg Vorbis: usable ^>= 2(~96kb/s); recommended ^>= 4(~128kb/s)
echo Default: 3(~112kb/s)
set /p abt=Vorbis quality (-1..10)[%abt%]:
if "%abt%" EQU "" (
set acodec=libvorbis
goto audioend
)
if %abt% LSS -1 goto badaudio
if %abt% GTR 10 goto badaudio
set acodec=libvorbis -q:a %abt%
goto audioend
:mp3
set abt=
echo Default: 128kb/s CBR
set /p abt=mp3 bitrate ABR in kb/s (32..320)[%abt%]:
if "%abt%" EQU "" (
set acodec=libmp3lame
goto audioend
)
if %abt% LSS 32 goto badaudio
if %abt% GTR 320 goto badaudio
set acodec=libmp3lame -abr 1 -b:a %abt%k
goto audioend
:audioend
echo %acodec%
echo.
set file=0
:next
if "%~1"=="" goto :end
set /a file=file + 1
title ffmpeg(%file%): %~nx1
set nrpl=0
if exist "%~dpn1.%ext%" (goto plus) else (
set out="%~dpn1.%ext%"
goto skiplus
)
:plus
set /A nrpl=nrpl+1
if exist "%~dpn1_(%nrpl%).%ext%" (goto plus) else set out="%~dpn1_(%nrpl%).%ext%"
:skiplus
rem -pix_fmt yuvj420p
rem wav2dvd -af pan=5.1:c0=c0:c1=c2:c2=c1:c3=c5:c4=c3:c5=c4
rem start /b /low /wait
rem -v verbose :flags=lanczos+print_info -map 0:v -map 0:a
%ffmpeg% -i "%~1" -threads 0 -vf %prefilter%,scale=trunc((%width%+1)/2)*2:trunc(((ih/sar*ow/iw)+1)/2)*2,setsar=1 -pix_fmt yuv420p -c:v libx264 -crf %vbt% -preset %preset% %tune% -c:a %acodec% -sn %out%
shift
goto next
:end
title ffmpeg(%file%): DONE
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment