Skip to content

Instantly share code, notes, and snippets.

@cetinajero
Last active May 22, 2024 17:20
Show Gist options
  • Save cetinajero/776fea1589b37340e7f39089053bdf17 to your computer and use it in GitHub Desktop.
Save cetinajero/776fea1589b37340e7f39089053bdf17 to your computer and use it in GitHub Desktop.
Usefull ffmpeg commands

Usefull ffmpeg commands

General options

List codecs

ffmpeg -codecs | grep -i sub

Quiet output:

ffmpeg -i input.avi -loglevel fatal -stats output.mp4

Search for more streams by analyzing more video:

ffmpeg -i input.avi -analyzeduration 300M -probesize 300M output.mp4

Previewing with ffplay

ffplay -i input.avi -vf crop=ih

Control size of the output

Define specific codecs

ffmpeg -i input.avi -c:v:0 h264 -c:a:0 aac output.mp4

Resize video mantaining aspect ratio

ffmpeg -i input.avi -vf scale=-1:720 output.mp4

Define bitrate

ffmpeg -i input.avi -vcodec h264 -b:v:0 250k -acodec aac -b:a:0 128k output.mp4

Export only a part of the video

Export only the first 30 seconds:

ffmpeg -i input.avi -t 30 output.mp4

Export only the defined section:

ffmpeg -i input.avi -ss 00:04:28 -to 00:55:06 output.mp4

Note: If speed is important... use -c copy to avoid encoding.

Map streams

Useful for removing and reordering streams:

  • Identifier: (source:type:number)
ffmpeg -i input.avi -map 0:v:0 -map 0:a:2 -map 0:a:0 output.mp4

Crop a video

Use cropdetect to auto-detect crop size:

ffplay -i input.avi -vf cropdetect

Use the crop filter:

  • crop=w:h:x:y

These 3 are the same:

ffmpeg -i input.avi -vf crop=640:360:1280:250 output.mp4
ffmpeg -i input.avi -vf crop=16*10*4:9*10*4:1920-16*10*4:250 output.mp4
ffmpeg -i input.avi -vf crop=iw/3:ih/3:iw-ow:250 output.mp4

The filter will automatically center the crop if x and y are omitted:

  • crop=w:h

Crop 10 pixels from the top, and 10 from the bottom:

ffmpeg -i input.avi -vf crop=iw:ih-2*10 output.mp4

Note: If speed is important... use -c:a copy to avoid audio encoding.

Adjust FPS

ffmpeg -i input.avi -vf fps=10 output.mp4

Multiplex three independent sources

ffmpeg -i input.avi -i left.wav -i right.wav -map 0 -map 1 -map 2 -c copy output.mp4

Concatenate three independent sources

Audio & Video

ffmpeg -i input1.mp4 -i input2.mkv -i input3.mov -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[v][a]" -map "[v]" -map "[a]" output.mp4

Just audio

ffmpeg -i input1.wav -i input2.wav -i input3.wav -i input4.wav -i input5.wav -i input6.wav -filter_complex "[0:a:0][1:a:0][2:a:0][3:a:0][4:a:0][5:a:0]concat=n=6:v=0:a=1 [a]" -map "[a]" output.mp3

Extirp a section from an audio

REMOVE_FROM="00:00:06"
REMOVE_TO="00:00:08.8"

ffmpeg -i input.mp3 -c copy -to $REMOVE_FROM input-temp1.mp3; \
ffmpeg -i input.mp3 -c copy -ss $REMOVE_TO input-temp2.mp3; \
ffmpeg -i input-temp1.mp3 -i input-temp2.mp3 \
       -filter_complex "[0:a:0][1:a:0]concat=n=2:v=0:a=1 [a]" \
       -map "[a]" output.mp3; \
rm input-temp*.mp3;

Convert audio 5.1 to stereo

Convert multichannel audio to stereo (Audio channels)

ffmpeg -i input.avi -ac:a:0 2 output.mp4

Define specific levels of audio channels to stereo (Audio filter)

  • FL/FC/BL: Front/Center/Back Left
ffmpeg -i input.avi -af "pan=stereo|FL=FC+0.30*FL+0.30*BL|FR=FC+0.30*FR+0.30*BR" output.mp4

Adjust volume

Analyze the file using the volumedetect filter

ffmpeg -i input.mp3 -af volumedetect -f null - 2>&1 | grep volume:

Adjust the volume by 10% or 5dB down

ffmpeg -i input.mp3 -af "volume=0.9" output.mp3

# or

ffmpeg -i input.mp3 -af "volume=-5dB" output.mp3

Fade in and fade out effects

Start with a black screen and fade into full view over 3 seconds

ffmpeg -i input.avi -vf "fade=t=in:st=0:d=3" output.mp4

Start fading to black over 5 seconds at the 10-second mark

ffmpeg -i input.avi -vf "fade=t=out:st=10:d=5" output.mp4

The audio file will start at a low volume and fade in to full over 5 seconds.

ffmpeg -i input.mp3 -af "afade=t=in:st=0:d=5" output.mp3

The audio file will start fading to zero volume over 5 seconds starting at the 15-second mark.

ffmpeg -i input.mp3 -af "afade=t=out:st=15:d=5" output.mp3

You can apply both fade in and out effects at the same time, to avoid having to re-encode twice.

ffmpeg -i input.avi -vf "fade=t=in:st=0:d=3,fade=t=out:st=10:d=5" output.mp4

Slow motion / Speed up / Timelapse

Slow mo:

ffmpeg -i input.avi -vf "setpts=PTS/0.5" -af "atempo=0.5" output.mp4

Speed up:

ffmpeg -i input.avi -vf "setpts=PTS/2" -af "atempo=2.0" output.mp4

NOTE: The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input). If you need to, you can get around this limitation by stringing multiple atempo filters together. The following with quadruple the audio speed:

ffmpeg -i input.avi -vf "setpts=PTS/4" -af "atempo=2.0,atempo=2.0" output.mp4

Timelapse:

  • Speed: 60x (1 hour in 1 min, 1 min in 1 sec)
  • Audio: No
ffmpeg -i input.avi -vf "setpts=PTS/60" -an output.mp4

Convert DVD VIDEO_TS folder to video format

cat ./VIDEO_TS/*.VOB | ffmpeg -i - output.mp4

Download Streaming videos from Salesforce @ Amazon CloudFront

ffmpeg -i rtmp://s39mvapi4g5wnh.cloudfront.net/cfx/st/flv:VideosPortalNuevo/Finanzas/Revaluacion_Moneda_Extranjera -profile:v high -preset slow -threads 0 Revaluacion_Moneda_Extranjera.mp4

Convert a RTSP url to RTMP url

ffmpeg -i "[your rtsp link]" -f flv -r -s -an "[Your rtmp link]"

Sync subtitles

http://subshifter.bitsnbites.eu

Download YouTube files (youtube-dl)

MP3 Playlist

youtube-dl --extract-audio --audio-format mp3 -i -o "%(title)s.%(ext)s" https://www.youtube.com/watch?list=RDQMYgJ4UDCmSlA

MP4 Video

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' -o "%(title)s.%(ext)s" https://www.youtube.com/watch?v=R8wG1W1TPgw

Referred Video

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' -o "%(title)s.%(ext)s" --referer https://www.taitradio.com/products/tait-enable-network-management/enablemonitor https://player.vimeo.com/video/106990265

Split an MP3 file by detecting silent parts

mp3splt is a program for splitting mp3s without re-encoding that has a silence detector (among other detection methods).

Usage with the Silence mode (-s):

mp3splt -s input.mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment