Skip to content

Instantly share code, notes, and snippets.

@travelhawk
Last active September 4, 2024 13:13
Show Gist options
  • Save travelhawk/4537a79c11fa5e308e6645a0b434cf4f to your computer and use it in GitHub Desktop.
Save travelhawk/4537a79c11fa5e308e6645a0b434cf4f to your computer and use it in GitHub Desktop.
Collection of ffmpeg commands (basic usage, streaming, advanced usage)

ffmpeg

ffmpeg is a fast video and audio converter that can also grab from a live audio/video source.

Standard usage

Getting help and information

  • -h show all options
  • -h(elp) topic show help
  • -version show version
  • -formats show available formats
  • -codecs show available codecs
  • -decoders show available decoders
  • -encoders show available encoders
  • -protocols show available protocols
  • -filters show available filters
  • -pix_fmts show available pixel formats
  • -sample_fmts show available audio sample formats
  • -colors show available color names

compress video using bitrate per second

ffmpeg -i input.mp4 -b 800k output.mp4

save camera stream to file

ffmpeg -f dshow -i video="Integrated Camera" out.mp4

downsample input frames from 30fps to 10fps

ffmpeg -f dshow -framerate 30 -i video="XX" -r 10 output.mp4

change container

Change container from mkv to mp4

ffmpeg -i file.mkv -acodec copy -vcodec copy file.mp4

list dshow input devices

ffmpeg -list_devices true -f dshow -i dummy

list capabilites / device options for a specific device

ffmpeg -f dshow -list_options true -i video="MiraBox Video Capture"

Advanced

creating multiple outputs

ffmpeg supports multiple outputs created out of the same input(s) in the same process. The usual way to accomplish this is:

ffmpeg -i input \
	-s 1280x720 -acodec … -vcodec … output1 \
	-s 640x480  -acodec … -vcodec … output2 \
	-s 320x240  -acodec … -vcodec … output3

divide input resolution by half

ffmpeg -i input.avi -vf scale="iw/1:ih/2" output.avi

stream local file to output pipe

ffmpeg "-re -i sunflowers.mp4 -f mpegts -codec:v mpeg1video -s 1280x720 -b:v 2500k -bf 0 pipe:1"

fix broken avi to mpegts mp4

ffmpeg -i broken_file.avi -f mpegts -bf 0 video_fixed.mp4

Streaming

camera stream and play to/from local udp server

ffmpeg -f vfwcap -framerate 25 -video_size 640x480 -i 0 -vcodec mpeg4 -f mpegts udp://127.0.0.1:23000
ffplay udp://127.0.0.1:23000

camera stream to http streaming server

ffmpeg -f vfwcap -framerate 25 -video_size 640x480 -i 0 -f mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 http://localhost:8081/stream

camera stream to http streaming server (including audio)

ffmpeg -f vfwcap -framerate 25 -video_size 640x480 -i 0 -f alsa -ar 44100 -c 2 -i hw:0 -f mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 -codec:a mp2 -b:a 128k -muxdelay 0.001 http://localhost:8081/stream

input stream to http streaming server (original audio)

ffmpeg -stdin -f s16le -ar 48k -ac 2 -i pipe:0 -acodec pcm_u8 -ar 48000 -f aiff pipe:1

ffmpeg camera stream to rtmp

ffmpeg -f dshow -framerate 30 -video_size 800x600 -i video="MiraBox Video Capture" -vcodec libx264 -preset ultrafast -tune zerolatency -b:v 900k -f flv "rtmp://localhost/live/stream"

ffmpeg stream to rtmp with buffer size

ffmpeg -f dshow -framerate 30 -video_size 800x600 -rtbufsize 702000k -i video="MiraBox Video Capture" -vcodec libx264 -preset ultrafast -tune zerolatency -b:v 900k -f flv "rtmp://localhost/live/stream"

ffmpeg stream to jsmpeg

ffmpeg -i -f dshow -vcodec -fmt_pix nv12 rawvideo -i pipe:0 -f mpegts -codec:v mpeg1video -s 896x504 -b:v 3500k -bf 0 pipe:1

use ffmpeg as an rtmp server (input and output)

ffmpeg -f flv -listen 1 -i rtmp://localhost:8889/live/app -c copy -f flv -listen 1 rtmp://localhost:1935/live/app


Dealing with raw video

convert raw YUV (NV12) format to streamable mpegts format

ffmpeg -pix_fmt nv12 -s 352x288 -i foreman_352x288.yuv -f mpegts -bf 0 video.mp4

test whether nv12 format is playable

mplayer -demuxer rawvideo -rawvideo w=352:h=288:format=nv12 file.yuv ffplay -s 352x288 -pix_fmt nv12 file.yuv

test with raw videos

ffmpeg -y -s 896x504 -f rawvideo -pix_fmt nv12 -i streamfile-video.mp4 streamfile.mp4


Use with nodeJS

execute ffmpeg

var ffmpeg = children.spawn('ffmpeg.exe' ...)
ffmpeg.stdin.write(message.binaryData);

Other commands

Encode a video for Sony PSP

ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp

Add subtitles to your video

ffmpeg -i input.mp4 -i subtitles.srt -c copy -c:s mov_text output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment