Skip to content

Instantly share code, notes, and snippets.

@luciovilla
Last active July 15, 2021 21:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luciovilla/ea9c261978b244f479d23f5309818594 to your computer and use it in GitHub Desktop.
Save luciovilla/ea9c261978b244f479d23f5309818594 to your computer and use it in GitHub Desktop.
Useful ffmpeg commands for newsrooms

Useful ffmpeg commands for newsrooms

Quickly resize, convert, extract audio from videos and edit audio from the command line.

About ffmpeg

A complete, cross-platform solution to record, convert and stream audio and video. https://ffmpeg.org

Installing

Install Homebrew first

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then install ffmpeg with Homebrew

$ brew install ffmpeg

Commands

Resize video down to 640x480

$ ffmpeg -i input.mp4 -s 640x480 output.mp4

Convert to webm

$ ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm

Remove audio from video

$ ffmpeg -i input.mp4 -vcodec copy -an output.mp4

Convert video from mp4 to avi

$ ffmpeg -i input.mp4 output.avi

Extract audio (mp3) from video

$ ffmpeg -i video.mp4 -vn -ab 256 audio.mp3

Adding a fade to black

  • make sure you know the frame count and how many frames you want it to last
ffmpeg -i video_file.mp4 -y -vf fade=out:120:30 output_file.mp4

Converting WAV audio file to MP3

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3

Save the first frame of video file as jpg

i=1
for frame in *.mov; do
 ffmpeg -i $frame -vframes 1 -f image2 $i.jpg; i=$((i+1))
done

Get the left channel audio and convert to mono

ffmpeg -i input.wav -map_channel 0.0.0 output.wav

Get the right channel audio and convert to stereo MP3

ffmpeg -i input.wav -map_channel 0.0.1 -map_channel 0.0.1 -c:v copy output_right.mp3

Extract the first 30 seconds from the audio file

  • -ss indicates the starting time (00:00:00)
  • -to indicates the end time (00:00:30)
ffmpeg -i input.mp3 -ss 00:00:00 -to 00:00:30 -c copy output.mp3

To slow down your video, you have to use a multiplier greater than 1:

ffmpeg -i INPUT.mp4 -filter:v "setpts=2.0*PTS" OUTPUT.mp4

Concatenation of files with same codecs:

  • Create a file mylist.txt with all the files you want to have concatenated in the following form:
# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
  • Create a mylist.txt file with all the files in its directory with bash:
for f in ./*.mp4; do echo "file '$f'" >> mylist.txt; done
  • Command to concatenate files from mylist.txt
ffmpeg -f concat -safe 0 -i mylist.txt -c copy OUTPUT.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment