User Tools

Site Tools


video:encoding

Drop every second frame

ffmpeg.exe -i input.mkv -sws_flags lanczos+full_chroma_inp -filter:v select="mod(n-1\,2)",fps=29.97 -c:v h264_nvenc -preset:v p7 -rc 0 -rc:v constqp -init_qpI 20 -init_qpP 20 -init_qpB 23 -g 240 -b_ref_mode 2 -pix_fmt yuv420p -color_range tv -color_primaries bt709 -color_trc bt709 -colorspace bt709 -c:a copy output.mkv

7.1 channel sound to 5.1

You must add for proper channel mapping

-ac 6

Encode VP9 maximum quality

ffmpeg -i input.mkv -pix_fmt yuv420p10le -c:v libvpx-vp9 -pass 1 -speed 8 -quality good -threads 16 -row-mt 1 -crf 25 -b:v 0 -g 240 -an -f webm nul
ffmpeg -i input.mkv -pix_fmt yuv420p10le -c:v libvpx-vp9 -pass 2 -deadline best -threads 16 -row-mt 1 -crf 25 -b:v 0 -g 240 -codec:a libopus -ab 128k output.webm

FFMPEG and NVENC

nVidia -hwaccel cuvid -c:v h264_cuvid
Start from frame -ss 5
HQ framerate 30fps -r ntsc
Use photoshop levels -vf curves=psfile=input.acv
Deblocking, Autolevels ,pp=ha/va/al
Convert colors -vf colormatrix=bt709:bt601

Show codec parameters

ffmpeg -h encoder=h264_omx

Quality based encoder NVENC (Ampere)

h264crf.bat
ffmpeg -i "%~n1%~x1" -sws_flags lanczos+full_chroma_inp -filter:v colorlevels=rimax=0.99:gimax=0.99:bimax=1.0,crop=iw:iw/2.666666,scale=-1:720 -c:v h264_nvenc -preset:v p7 -rc 0 -rc:v constqp -init_qpI 20 -init_qpP 20 -init_qpB 23 -g 240 -coder 2 -b_ref_mode 2 -pix_fmt yuv420p -color_range tv -color_primaries bt709 -color_trc bt709 -colorspace bt709 -af dynaudnorm=m=4 -c:a aac -b:a 160k "nvenc__%~n1%~x1.mp4"
h265crf.bat
ffmpeg -i input.mp4 -c:v hevc_nvenc -preset:v p7 -profile:v main10 -rc:v constqp -rc-lookahead 1 -spatial-aq 0 -temporal-aq 1 -weighted_pred 0 -init_qpI 20 -init_qpP 20 -init_qpB 23 -b_ref_mode 2 -dpb_size 3 -multipass 0 -g 240 -bf 5 -pix_fmt p010le -color_range tv -color_primaries bt709 -color_trc bt709 -colorspace bt709 -movflags +faststart -codec:a copy output.mp4
h265hdrcrf.bat
ffmpeg -i input.mkv  -c:v hevc_nvenc -preset:v p7 -profile:v main10 -rc:v constqp -rc-lookahead 1 -spatial-aq 0 -temporal-aq 1 -weighted_pred 0 -init_qpI 20 -init_qpP 20 -init_qpB 23 -b_ref_mode 2 -dpb_size 3 -multipass 0 -g 240 -bf 5 -pix_fmt p010le -codec:a copy  -color_primaries bt2020 -colorspace bt2020nc -color_trc smpte2084 -bsf:v hevc_metadata=transfer_characteristics=16:colour_primaries=9:video_full_range_flag=0:matrix_coefficients=9 output.mkv

Create GIF

creategif.bat
@set /p CLUT=How Many Colors (4..256): 
@echo Number of colors: %CLUT%
 
@set /p BS=Bayer_Strength (0..5 lower is stronger): 
@echo Bayer Strength: %BS%
 
@ffmpeg.exe -y -stats -hide_banner -i "%1" -an -sn -dn -sws_dither ed -filter_complex "crop=1920:784:0:148,scale=640:-2:sws_flags=spline+accurate_rnd+full_chroma_int+full_chroma_inp:in_range='mpeg/tv':out_range='jpeg/full/pc',hqdn3d=5,smartblur=lr=1.75:ls=-0.50:lt=-5.5:cr=0.5:cs=1.0:ct=0.5,minterpolate=fps=25/2:mi_mode=mci:me_mode=bidir:me=epzs:vsbmc=1,scale=320:-2:sws_flags=spline+accurate_rnd+full_chroma_int+full_chroma_inp,xbr=2,scale=iw/2:-2:sws_flags=spline+accurate_rnd+full_chroma_int+full_chroma_inp,format=pix_fmts=rgb444le,split=2[vid][clut];[clut]palettegen=max_colors=%CLUT%:reserve_transparent=0:stats_mode=diff[out2]" -map [vid] -c:v ffv1 "%~n1_ll.mkv" -map [out2] "%~n1_CLUT%CLUT%_D.png"
 
@ffmpeg.exe -y -stats -hide_banner -i "%~n1_ll.mkv" -i "%~n1_CLUT%CLUT%_D.png" -lavfi "paletteuse=dither=bayer:bayer_scale=%BS%" -loop 0 -vsync 0 "%~n1_CLUT%CLUT%_D_BS%BS%.gif"
 
@rem  Optimizing GIF...
@gifsicle.exe -V -O3 "%~n1_CLUT%CLUT%_D_BS%BS%.gif" -o "%~n1_CLUT%CLUT%_D_BS%BS%_o.gif"

Defish

ffmpeg -i "$1" \ -filter_complex 'extractplanes=y+u+v[y][u][v]; [u] scale=w=3840:h=2160:flags=print_info+neighbor+bitexact [us]; [v] scale=w=3840:h=2160:flags=print_info+neighbor+bitexact [vs]; [y][us][vs]mergeplanes=0x001020:yuv444p,format=pix_fmts=yuv444p10le,hqdn3d=3:3:5:5,frei0r=lenscorrection:0.5:0.5:0.16:0.6,scale=w=1920:h=1080:flags=print_info+bicubic+full_chroma_inp+full_chroma_int' \ -sws_dither none \ -q 0 -quant_mat hq \ -c:v prores_ks \ -profile:v 4 \ -c:a copy \ -c:s copy \ -c:d copy \ -map 0 \ "$2/$1"

23fps to 60fps

23,976 / 60 = 0.3996 (39.96%)
Speed up 239.76%

Rotate 90degree

rotate90.bat
ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=90 output.mp4

Real rotate

-vf "transpose=2"

Create slideshow from pictures

slideshow.bat
c:\programs\stream\ffmpeg -f concat -i list.txt -codec:v libx265 -b:v 1000k -x265-params "pass=1" -pix_fmt yuv420p10le -preset fast -filter:v "crop=in_w:in_h-480" -maxrate 20000k -sws_flags spline -s 3840x2160 -aspect 16:9 -f mp4 nul
c:\programs\stream\ffmpeg -f concat -i list.txt -codec:v libx265 -b:v 1000k -x265-params "pass=2" -pix_fmt yuv420p10le -preset fast -filter:v "crop=in_w:in_h-480" -maxrate 20000k -sws_flags spline -s 3840x2160 -aspect 16:9 -f mp4 output.mp4

Transform bt601 to bt709

-vf "colormatrix=bt601:bt709"
list.txt
file '0.jpg'
duration 10
file '0_02.jpg'
duration 10

2-pass x264

x264hq.bat
c:\programs\stream\ffmpeg -y -i input.mov -vf curves=psfile=input.acv -preset veryslow -pix_fmt yuvj420p -c:v libx264 -b:v 30000k -pass 1 -an -f mp4 nul
c:\programs\stream\ffmpeg -i input.mov -vf curves=psfile=input.acv -preset veryslow -pix_fmt yuvj420p -c:v libx264 -b:v 30000k -pass 2 -c:a aac -b:a 320k output.mp4

Archiving in HEVC

x265archive.bat
c:\programs\stream\ffmpeg -i "uhqresize.avs" -codec:v libx265 -crf 12 -preset slow -c:a libopus -b:a 320k output.mkv

Rip to MP3

ripmp3.bat
c:\programs\stream\ffmpeg -i video.mp4 -b:a 192K -vn music.mp3

Recode audio to AC3

convertac3.bat
ffmpeg -i FILE.mkv -map 0:v -map 0:a:0 -map 0:s -c copy -c:a ac3 -b:a 640k -cutoff 18000 FILE-AC3.mkv

Convert HDR to SDR

ffmpeg.exe -i input.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,
zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,
format=yuv420p -c:v libx265 -crf 18 -preset slower output.mkv

Encode HDR

ffmpeg -i source.webm -c:v libx265 -pix_fmt yuv420p10le -x265-params "level=5.2:colorprim=bt2020:colormatrix=bt2020nc:transfer=smpte2084" -crf 12 -preset medium -c:a copy output.mkv

Patch some old videos

c:\programs\stream\ffmpeg -fflags +genpts -r 30 -i "input.mkv" -codec copy -bsf:v mpeg4_unpack_bframes output.mkv

Encode Dolby Pro Logic

ffmpeg -i input.mp4 -c:v copy -c:a:0 aac -ac 2 -b:a 320k -af "aresample=matrix_encoding=dplii" dolby-prologic.mp4

Extract core from TrueHD

ffmpeg -i input.mkv -map 0:V:0 -map 0:a:0 -c:v copy -bsf:a truehd_core -c:a copy output.mkv

Double framerate by motion

ffmpeg -i input.hevc -filter “minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1'” output.hevc

Export video for Premiere almost lossless

c:\programs\stream\ffmpeg -i input.mp4 -c:v v210 -c:a copy output.avi

Insert HDR10 Metadata

mkvmerge -o output.mkv --colour-matrix 0:9 --colour-range 0:1 --colour-transfer-characteristics 0:16 --colour-primaries 0:9 --max-content-light 0:1000 --max-frame-light 0:128 --max-luminance 0:1000 --min-luminance 0:0.005 --chromaticity-coordinates 0:0.68,0.32,0.265,0.690,0.15,0.06 --white-colour-coordinates 0:0.3127,0.3290 input.mkv

Insert Stereo into 5.1 Layout

c:\programs\stream\ffmpeg -i input.mkv -filter_complex "[0:a]pan=5.1(side)|FL=FL|FR=FR|FC=FL+FR|LFE<FL+FR|SL=0.7*FL|SR=0.7*FR[a]" -map 0 -map -0:a -map "[a]" -c copy -c:a ac3 -b:a 640k output.mkv

Normalize audio

ffmpeg -i video.mkv -af "volumedetect" -vn -sn -dn -f null /dev/null
ffmpeg -i input.mkv -af "volume=5dB" output.mkv

Dynamic Normalization

ffmpeg -i input.mkv -af "dynaudnorm=m=3" output.mkv

Save frames from video

c:\programs\stream\ffmpeg -i input.mp4 -vf fps=10/60 -qscale:v 2 image-%%03d.jpg

Encode pictures to video

ffmpeg -framerate 25 -i %%06d.png -pix_fmt yuv420p -vf crop=3840:1998:0:161,curves=psfile=enc.acv,smartblur=lr=2.00:ls=-0.1:lt=-5.0:cr=2:cs=-0.1:ct=-1.0 -codec:v libx265 -x265-params "crf=22:keyint=340:vbv-maxrate=60000:vbv-bufsize=65535" -preset veryslow output.mkv

Decode pictures from video

ffmpeg -i input.mkv -qscale:v 2 image-%%06d.jpg

Encrypt

ffmpeg -i input.mp4 -vcodec copy -acodec copy -encryption_scheme cenc-aes-ctr -encryption_key 100b6c20940f779a4589152b57d2dacb -encryption_kid eb676abbcb345e96bbcf616630f1a3da encrypted.mp4

Inverse Telecine

ffmpeg -i input.mpg -vf fieldmatch,yadif=deint=interlaced,decimate -pix_fmt yuv420p10le -codec:v libx265 -x265-params "crf=20:keyint=340:vbv-maxrate=60000:vbv-bufsize=65535"  -preset superfast output.mkv

Tune

You can optionally use -tune to change settings based upon the specifics of your input. Current tunings include:

film use for high quality movie content; lowers deblocking
animation good for cartoons; uses higher deblocking and more reference frames
grain preserves the grain structure in old, grainy film material
stillimage good for slideshow-like content

Transform HLG to HDR10

-vf zscale=transfer=smpte2084

Encode with HDR10 Metadata

Color Primaries: bt2020
Mastering Display: Display P3
Luminance: min:0.001cm/m2 max: 1000cd/m2
1000 / 400 average

x265-params "crf=21:keyint=340:vbv-maxrate=60000:vbv-bufsize=65535:colorprim=bt2020:colormatrix=bt2020nc:transfer=smpte2084:hdr=1:info=1:repeat-headers=1:max-cll=1000,400:max-fall=400:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1)"


bt2020
master-display=G(8500,39850)B(6550,2300)R(35400,14600)WP(15635,16450)L(10000000,1)

video/encoding.txt · Last modified: 2024/05/03 10:32 by Jan Forman