Skip to content

Instantly share code, notes, and snippets.

@Postrediori
Last active August 8, 2024 09:10
Show Gist options
  • Save Postrediori/dc7ed8fe68ebf86e126809bde350f036 to your computer and use it in GitHub Desktop.
Save Postrediori/dc7ed8fe68ebf86e126809bde350f036 to your computer and use it in GitHub Desktop.

ffmpeg HowTos

Contents

How to downsample 4k to 1080p using ffmpeg while maintaining the quality?

ffmpeg -i orig.mp4 -vf scale=1920:1080 -c:v libx264 -crf 20 -preset slow smaller.mp4

Converting video from 1080p to 720p with smallest quality loss

ffmpeg \
  -i orig.mp4 \
  -vf scale=-1:720 -c:v libx264 -crf 18 -preset veryslow -c:a copy \
  -y out-720p.mp4

How to speed up/slow down a video

To double the speed of the video use setpts filter with multiplier 0.5:

ffmpeg -i input.mkv -filter:v "setpts=0.5*PTS" output.mkv

To slow down video use a multiplier greater than 1:

ffmpeg -i input.mkv -filter:v "setpts=2.0*PTS" output.mkv

Remove audio from video file with FFmpeg

ffmpeg -i example.mkv -c copy -an example-nosound.mkv

Convert frames to optimized gif with palette generation

ffmpeg -i frames/frame%04d.png -filter_complex "[0:v] palettegen" -y palette.png
ffmpeg -i frames/frame%04d.png -i palette.png -y video.gif

Create repeating copy of a video until the ffmpeg process is stopped

ffmpeg -stream_loop -1 -i input.mp4 -c copy output.mp4

Merge audio and video by shortest input

ffmpeg -i "videoFile.mp4" -i "audioFile.mp3" -shortest outPutFile.mp4

Combine an image & an audio file to make a video

ffmpeg -loop 1 -i background.jpg -i soundtrack.wav -c:v libx264 -tune stillimage -pix_fmt yuv420p -c:a aac -b:a 192k -shortest output.mp4

How to create a video from multiple images

ffmpeg -framerate 12 -i frames/frame%04d.jpg -i soundtrack.aac -c:v libx264 -pix_fmt yuv420p -c:a copy output.mkv

Create video from images starting for a number (e.g. with enumerated images from a photocamera):

ffmpeg -framerate 12 -start_number 1729 -i IMG_%d.jpg -c:v libx264 -pix_fmt yuv420p output.mkv

Replace audio in a video

Replace audio track with another file:

ffmpeg -i v.mp4 -i a.wav -c:v copy -map 0:v:0 -map 1:a:0 new.mp4

Replace audio and reencode it to AAC:

ffmpeg -i v.mp4 -i a.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k -shortest new.mp4

Convert audio file to MP3

ffmpeg -i input.wav -c:a mp3 -b:a 192k output.mp3

How to use FFmpeg Command for Reverse Video

Reverse only video

ffmpeg -i video.mp4 -vf reverse reversed.mp4

Reverse audio and video

ffmpeg -i video.mp4 -vf reverse -af areverse reversed.mp4

Extract subtitles from video

ffmpeg -i Movie.mkv -map 0:s:0 subs.srt

Here s:0 sets the subtitle stream id.

FFMPEG is doubling audio length when extracting from video

Specify bitrate when converting the file:

ffmpeg -i "video.webm" -acodec mp3 -ab 128k "audio.mp3"

Crossfade effect with ffmpeg

Concat files with ffmpeg

Prepare a concat file (e.g. concat.txt) with list of fragments:

file '/path/to/1st/file.mp4'
file '/path/to/2nd/file.mp4'
file '/path/to/3rd/file.mp4'
...

Run concat plugin:

ffmpeg -f concat -safe 0 -i concat.txt -c copy output.mp4

Cut video fragments

Specify start of a fragment with -ss flag and duration with -t. Note -c copy is specified here for simplicity as it may produce slightly corrupter results when not aligned with key frames. In more general case, us

ffmpeg -i MVI_9437.MP4 -ss 00:00:00.0 -t 00:00:24.0 -c copy MVI_9437_1.MP4
ffmpeg -i MVI_9437.MP4 -ss 00:01:27.0 -t 00:01:10.0 -c copy MVI_9437_2.MP4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment