Skip to content

Instantly share code, notes, and snippets.

@daephx
Last active May 22, 2024 17:27
Show Gist options
  • Save daephx/db271a2c10beea4869d7aa1a0c2d2750 to your computer and use it in GitHub Desktop.
Save daephx/db271a2c10beea4869d7aa1a0c2d2750 to your computer and use it in GitHub Desktop.
[FFmpeg Examples] Common FFmpeg Commands #Commands #Examples

FFmpeg Examples

Personal collection of basic examples / scripts.

FFmpeg Documentation
A complete, cross-platform solution to record, convert and stream audio and video.

Table of contents


Convert Media

Various converstion commands between audio, images & video.

Generate color pallette for improved GIF quality

ffmpeg -y -i "input.mp4" -vf palettegen "palette.png"

Convert MP4 to GIF -- Specifying a Framerate and Resolution

ffmpeg -y -i "input.mp4" -i palette.png -filter_complex paletteuse -r 10 -s "320x480" "output.gif"

Loop image to Audio

ffmpeg -ignore_loop 0 -i "image.gif" -i "audio.mp3" -c:a copy -c:v libx264 -shortest "output.mp4"

More loop examples

ffmpeg -i "audio.mp3" -ignore_loop 0 -i "image.gif" -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -shortest -strict -2 -c:v libx264 -threads 4 -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "output.mp4"

ffmpeg -loop 1 -i "image.jpg" -i "audio.mp3" -shortest -acodec copy -vcodec mjpeg "output.mp4"

Crop Video Border

Remove black bars from edge of video files w/ preview.

1. Get crop parameters

ffmpeg -i "input.mp4" -vframes 10 -vf cropdetect -f null -

2. Preview with ffplay

ffplay -vf crop=1280:720:0:0 "input.mp4"

3. Re-encode using the crop filter

ffmpeg -i "input.mp4" -vf crop=1280:720:0:0 -c:a copy "output.mp4"

Rescale Video

Resize video file without cropping.

ffmpeg -i "input.mp4" -vf "pad=ih*16/9:ih:(ow-iw)/2:(oh-ih)/2,scale=1152:864" "ouput.mp4"

Split Timecode

Chop video duration based off timecode 'HH:MM:SS.ffff'

ffmpeg -ss 00:06:10 -i "input.mkv" -c copy -avoid_negative_ts make_zero "output.mkv"

Video Subtitles

Work with subtitles.

ffmpeg -i "input.mp4" -i "subtitles.srt" -c:s mov_text -c:v copy -c:a copy "output.mp4"

Create Slideshow from image sequence

Reference Video to make a picture slideshow with a single terminal command. The command below will take *.jpg pics in a directory and cycle through them one every four seconds (that is, framerate of 0.25):

cat *.jpg | ffmpeg -framerate 0.25 -f image2pipe -i - "output.mkv"

Or, add a music track as well:

cat *.jpg | ffmpeg -framerate 0.25 -f image2pipe -i SONG.mp3 -acodec copy "output.mkv"

Concatenation

Stitch videos together into one file.

<#
.SYNOPSIS
    Concatenate files using ffmpeg
.DESCRIPTION
    Concatenate means to link together or join.
    For example, concatenating these files below assuming they were 30sec in length?
    would result in an output file with a 90sec duration.
.NOTES
    Probably don't have to output temp.txt for concat
#>

$cat = "
file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4'
"
# Create 'cat.txt' example
($cat -split "[\r\n]+") | Set-Content $PSScriptRoot\cat.txt

# Example 'input' files clearly do not actually exist.
# ERROR is likely to occurs on 'ffmpeg call'.

# Concatenate the files
ffmpeg -f concat -safe 0 -i "cat.txt" -c copy "output.mp4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment