Skip to content

Instantly share code, notes, and snippets.

@viper25
Last active April 19, 2024 04:19
Show Gist options
  • Save viper25/286b27a41993eb9dfc5761bf2baa770f to your computer and use it in GitHub Desktop.
Save viper25/286b27a41993eb9dfc5761bf2baa770f to your computer and use it in GitHub Desktop.
Useful ffmpeg Commands

Extract YouTube Video

  • Download yt-dlp
  • Download Video: yt-dlp "https://www.youtube.com/watch?v=1Ut6RouSs0w" -o 1Ut6RouSs0w.webm
  • Run ffmpeg to cut your fragment: ffmpeg -ss 588.4 -t 15 -i 1Ut6RouSs0w.webm -y -c copy video-588.4-15-1Ut6RouSs0w.mp4

FFMPEG

https://ffmpeg.lav.io/ https://alfg.dev/ffmpeg-commander/ https://img.ly/blog/ultimate-guide-to-ffmpeg/#filtering-overview

https://ffmpeg.lav.io/ https://alfg.dev/ffmpeg-commander/

Reference

  • Remuxing is a lossless process that simply takes the video and audio streams from one container and puts them into a new container. -c:v copy to just re-mux the video instead of also re-encoding it.

Lossless file concatenation

Single Command

ls *.mp4 | sort -n | while read a; do echo "file '$a'"; done> list && ffmpeg -y -safe 0 -f concat -i list -c copy -c:a aac o.mp4
Sort by asc timestamp:
ls -lrt *.mp4 | while read a; do echo "file '$a'"; done> list && ffmpeg -y -safe 0 -f concat -i list -c copy -c:a aac OUT.mp4  

Manual

Concatenate files ffmpeg -f concat -safe 0 -i mylist.txt -c copy out.mp4

Create File List

dir /b > mylist.txt

Assuming a mylist.txt as:

file '20160911_192835.mp4'
file '20160911_193516.mp4'

concatenate as ffmpeg -f concat -i mylist.txt -c copy output.mp4

Remove audio

ffmpeg -f concat -safe 0 -i D:\junk\car_video\raw\mylist.txt -an -c copy D:\junk\car_video\raw\out.mov

Trim a video

Provide -ss BEFORE -i and -t else you'll get blank video first few sec.

Trim middle of video

(ss = start time and t = duration) ffmpeg -ss 00:00:00 -i input.mp4 -t 00:00:10 -codec copy out.mp4 The ss you can specify as 00:01:02 or 62

Trim first part of video

To trim upto (-to) 6 seconds of video: ffmpeg -i 02.mp4 -to 6 -c copy part1.mp4

Trim last part of video

Get last 6 seconds (-ss) of video: ffmpeg -i 02.mp4 -ss 6 -c copy part3.mp4

Converting video files to different formats

This just changes the container. ffmpeg automatically selects the appropriate codec for that container

ffmpeg -i video.mp4 out.avi
ffmpeg -formats

To change codec explicitly:

ffmpeg -i video.mp4 -vcodec libx264 out.avi

Converting video files to audio files

`ffmpeg -i input.mp4 -vn -ab 320 output.mp3`

Change resolution of video files

ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4

Extract MP3 from video file

ffmpeg -i input.mp4 -q:a 0 -map a output.mp3

Audio Encoding

Encode only Audio (c:a), leave Video stream as it is (copy)

ffmpeg -i "Z:\video1.avi" -c:v copy -c:a libfdk_aac "Z:\video1-Out.avi"

Encode Audio Track to MP3 (output file larger)

ffmpeg -i "Z:\video1.avi" -c:v copy -c:a libmp3lame -b:a 192k "Z:\video1-Audio-only.mp3"

Change just the audio codec to mp3

ffmpeg -i input.avi -acodec mp3 -vcodec copy out.avi

To loop over a set of files, create a bat file (else won't work) and enter the following contents. (in a bat file, use %%, when run from command line use %). Make the newfiles directory beforehand

for %A in ("*.mp4") do call "C:\Users\user1\Downloads\ffmpeg-20150702-git-03b2b40-win64-static\bin\ffmpeg.exe" -i "%A" -c:v copy -c:a libmp3lame -b:a 192k "newfiles\%%~nA.mp3"
pause

Loop over and convert flac to mp3. Input = "%%A". Output = %%~nA.mp3 for %%A in ("*.flac") do call "ffmpeg.exe" -i "%%A" -ab 320k -map_metadata 0 -id3v2_version 3 "newfiles\%%~nA.mp3"

Video Encoding

Encode Video and Audio streams ffmpeg -i "Z:\video1.avi" -c:v libx264 -c:a libmp3lame -b:a 320k "Z:\video1-Out.avi"

Change just the video codec ffmpeg -i input.avi -vcodec h264 out.avi Encode only Video to H.264 and leave Audio alone ffmpeg -i "Z:\video1.avi" -c:v libx264 -c:a copy "Z:\video1-out.avi"

Remove one audio stream from a file

See how many streams exist ffmpeg -i file.mp4 Output:

Stream #0.0: Video: mpeg4, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s
Stream #0.2: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s

Copy video stream and say, 2nd audio to new file ffmpeg -i file.mp4 -map 0:0 -map 0:2 -acodec copy -vcodec copy new_file.mp4

Compressing video files

ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4

REDUCE File size (Video bitrate from 3607 kb/s --> 1300k)

ffmpeg.exe -i "T:\Ghostbusters (2016).mkv"

Metadata:

encoder         : libebml v1.3.3 + libmatroska v1.4.4
creation_time   : 2016-09-30 06:02:49
Duration: 02:13:44.98, start: 0.000000, bitrate: 3607 kb/s

ffmpeg.exe -i "T:\Ghostbusters (2016).mkv" -b 1300k out.mp4

Speed up or Slow a video

Set setpts=0.5 & -r 60 (original 30 fps to 60fps) For 2X speed set to 1/2 or 0.5, for 3x, set to 1/3 or 0.3 ffmpeg -i 20170810_191555A.mov -r 60 -filter:v "setpts=0.5*PTS" output.mkv https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video https://www.ostechnix.com/20-ffmpeg-commands-beginners/

Rotate video

ffmpeg -i in.mov -vf "transpose=2" out.mov

0 = 90CounterCLockwise and Vertical Flip (default) 1 = 90Clockwise 2 = 90CounterClockwise 3 = 90Clockwise and Vertical Flip

Cut middle parts of video

Say you have 6 segments ABCDEF each 5 seconds long and you want A (0-5 seconds), C (10-15 seconds) and E (20-25 seconds) you'd do this: ffmpeg -i input.mp4 -t 5 a.tvshow -ss 10 -t 5 c.tvshow -ss 20 -t 5 e.tvshow or ffmpeg -i input.mp4 -t 0:00:05 a.tvshow -ss 0:00:10 -t 0:00:05 c.tvshow -ss 0:00:20 -t 0:00:05 e.tvshow Then join

Join an audio stream to a video stream (to the shortest stream)

ffmpeg -i out.mp4 -itsoffset 00:01:03 -i audio.wav -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 -async 1 -shortest out1.mp4

Split into 900 sec (15 min) files

ffmpeg -i input.mp3 -c copy -map 0 -segment_time 900 -f segment "input-%03d.mp3"

Convert videos to WhatsApp format

ffmpeg -i Rube.mp4 -vcodec libx264 -acodec aac output.mp4

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