Skip to content

Instantly share code, notes, and snippets.

@prabindh
Created June 3, 2021 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prabindh/c8048ac0f4cd6d48e9b682523e5b3c1f to your computer and use it in GitHub Desktop.
Save prabindh/c8048ac0f4cd6d48e9b682523e5b3c1f to your computer and use it in GitHub Desktop.
FFMPEG commands for multimedia operations, streaming, and interop with CUDA/OpenGL
#Creating SBS (side by side) videos:
ffmpeg -i input_file -i input_file2 -filter_complex hstack -vcodec libx264 -b:v 30M -vsync 0 output.mp4
#MP4 from raw YUV
ffmpeg -f rawvideo -vcodec rawvideo -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v libx264 -preset slow -qp 0 -vsync 0 -pix_fmt nv12 output.mp4
#Scaling:
ffmpeg -i input -filter:v scale=1280:-1 -sws_flags lanczos output
ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2" -preset slow -crf 18 output.mp4
ffmpeg -i input.mp4 -vf scale=1920:-1 -preset slow -crf 18 -vsync 0 out.mp4
#VP8 frames in IVF format to image file
ffmpeg -vcodec libvpx -i frames.ivf vp8decode.jpg
#Extract frames from video
(-start_number 0) to start filenames from index 0
ffmpeg.exe -i input.mp4 "<path>\%10d.png"
#Extract subrect video:
ffmpeg.exe -i in.mp4 -filter:v "crop=448:448:0:0" out_left_top.mp4
#Extract subrect to png:
ffmpeg.exe -i in.mp4 -filter:v "crop=448:448:0:0" -f image2 image-%03d.png
#Extract raw h264 (playable via vlc) from mp4:
ffmpeg -i "input.mp4" -vcodec copy -an -bsf:v h264_mp4toannexb test.h264
#Stream rtsp from testsrc:
ffmpeg -f lavfi -i testsrc -c:v libx264 -preset medium -vb 2000k -c:a aac -ab 128k -ar 44100 -bf 0 -f rtsp rtsp://ipaddr:8080/live/ffmpeg
#Stream from file to tcp at ip:port
ffmpeg -re -i output.mp4 -an -vcodec copy -f mpegts tcp://<ipaddr>:<port>
ffmpeg -re -i output.mp4 -vn -acodec copy -f mpegts tcp://<ipaddr>:<port>
Add "-stream_loop" to repeat
#Stream desktop to tcp at ip:port :
ffmpeg -y -rtbufsize 2000M -f gdigrab -framerate 30 -offset_x 0 -offset_y 0 -i desktop -c:v h264_nvenc -preset:v fast -vf scale=1920:1080 -f mpegts tcp://ip:port
#Capture desktop (windows):
ffmpeg -y -f gdigrab -i desktop -frames:v 1 screenshot.png
#Capture RTSP to .ts file:
ffmpeg -i rtsp://ip:port/some.amp -c copy -f mpegts out.ts
#Concatenate many files to single mp4:
Add file list to filelist.txt
ffmpeg -f concat -i filelist.txt -c copy outputbig.mp4
#Overlays: 2 streams, overlay
ffmpeg -y -i c:\outfile.mp4 -i c:\another.mp4 -i overlay.png -filter_complex "[0:v][0:a][1:v][1:a] concat=n=2:v=1:a=1 [outv] [outa]; [outv][2:v] overlay=main_w-overlay_w-10:10 [overlaid]" -map "[overlaid]" -map "[outa]" -g 60 -qp 0 -b:v 24M -vcodec h264_nvenc out.mp4
#VMAF comparison:
ffmpeg -i compare_input -i reference_input -lavfi "libvmaf=model_path='C\:/Users/fff/vmaf_float_v0.6.1.json':log_path='./vmaf.log':log_fmt=json:psnr=1:ssim=1:ms_ssim=1" -f null -
#C++ API for interoperation of CUDA+OpenGL+FFMPEG API
https://github.com/prabindh/cudammf
@prabindh
Copy link
Author

prabindh commented Sep 1, 2021

PSNR via filter complex
ffmpeg -i input_video.mp4 -i reference_video.mp4 -filter_complex "psnr" -f null NUL

@prabindh
Copy link
Author

Saving a frame from every 10 seconds.
ffmpeg -i input.mov -vf "select=not(mod(n,10))" -vsync vfr -q:v 2 img_%03d.jpg

@prabindh
Copy link
Author

prabindh commented Sep 8, 2022

HDR content in mp4, decode to png
ffmpeg -i hevc_10bit_hdr.mp4 -vf zscale=matrixin=2020_ncl,format=gbrp16le -an -frames:v 1 out_zscale_hdr.png

@prabindh
Copy link
Author

Wrapping raw h264 packets into mp4

ffmpeg -f h264 -i rawstream.h264 -c:v copy wrapped_file.mp4

@prabindh
Copy link
Author

Wrap a PNG to mp4
ffmpeg -framerate 30 -i input.png -c:v libx264 -pix_fmt yuv420p out.mp4

@prabindh
Copy link
Author

prabindh commented Jul 5, 2023

Generate MP4 from set of images

ffmpeg -f image2 -i /path/to/file/image%03d.jpg test.mp4

ffmpeg -f image2 -r 60 -i path/filename%03d.jpg -vcodec libx264 -crf 18 -pix_fmt yuv420p test.mp4

python generate filenames with str(number_to_use).zfill(3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment