Ahmad Awais

NAVIGATE


SHARE


My Command Line Audio Video Workflows with FFmpeg

Ahmad AwaisAhmad Awais

If you know anything about me then you know I love automating anything and everything. I even have a complete course on building automation CLI tools with Node.js.

In this post, I am sharing a couple of super useful ffmpeg workflows.

FFmpeg is a free and open-source software project consisting of a large suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg program itself, designed for command-line-based processing of video and audio files.

Converting videos and audio has never been this easy. I’m able to do a lot of excellent tricks with this command-line tool. Actually, that’s a good enough introduction โ€” let me share the real deal.

Gif to Video with FFmpeg#

# Gif to video.
# https://web.dev/replace-gifs-with-videos/
function gif2vid() {
	if [[ "-h" == "$1" ]]; then
		echo "-"
		echo "${wb}${bf}โ€” Help! โ€”${r}"
		echo "${wb}${bf}USAGE: rgif2vid some.gif nameOfVideos${r}"
		echo "-"
		return 1
	else
		ffmpeg -i "$1" "$2".mp4
		ffmpeg -i "$1" -c vp9 -b:v 0 -crf 41 "$2".webm
	fi
}

Cut 1 Minute from the start of a video with FFmpeg#

# Video cutting.
function cut1min() {
	for i in *.mp4;
	do name=`echo "$i" | cut -d'.' -f1`
	echo "$name"
	ffmpeg -ss 00:00:00.000 -i "$i" -t 60 -c:v libx264 -c:a copy "${name}-1min.mp4"
	done
}

Trim X seconds from the start of a video (FFmpeg-bar)#

# vidtrim <vid file name> <seconds to trim from start>
# vidtrim vid.mp4 5
function vidtrim() {
	ffmpeg-bar -i "$1" -ss "$2" -vcodec copy -acodec copy trimmed-"$1"
}

Join multiple videos with FFmpeg โ€” without re-encoding#

# vidjoin <intro vid file name> <actual vid file name>
# vidjoin intro.mp4 input.mp4
function vidjoin() {
	 echo file "$1" >> vidjoin.txt
	 echo file "$2" >> vidjoin.txt
	 ffmpeg-bar -f concat -i vidjoin.txt -c copy output.mp4
	 rm vidjoin.txt
}

Extract audio stream from video without re-encoding#

ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac

Rotate a video with FFmpeg#

# Rotate 90 clockwise:
ffmpeg -i in.mov -vf "transpose=1" out.mov

For the transpose parameter you can pass:
0 = 90 Counter Clockwise and Vertical Flip (default)
1 = 90 Clockwise
2 = 90 Counter Clockwise
3 = 90 Clockwise and Vertical Flip

Use -vf "transpose=2,transpose=2" for 180 degrees.

Convert an entire directory to one video with FFmpeg#

For Linux and macOS this can be done in one line, using parameter expansion to change the filename extension of the output file:

for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done

An alternate method would be like this:

for i in *.avi;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  ffmpeg -i "$i" "${name}.mov"
done

To convert with subdirectories use:

find . -exec ffmpeg -i {} {}.mp3 \;

Get FFmpeg information in a friendly way#

You can use -progress - to print-friendly info formatted by key=value.

ffmpeg -i vid.mp4 -progress - -y out.mp4

speed=3.15x
frame=458
fps=45.8
stream_0_0_q=39.0
bitrate=2337.0kbits/s
total_size=1231258682
out_time_ms=12323423
out_time=00:00:58.44534
dup_frames=0
drop_frames=0

Capture High-quality Images from a video with FFmpeg#

You can use -qscale:v (or the alias -q:v) to control image quality from a video as an output option.

Output a series of images#

ffmpeg -i input.mp4 -qscale:v 2 output_%03d.jpg

See the image muxer documentation for more options involving image outputs and their quality requirements.

Output a single image at ~60 seconds duration#

ffmpeg -ss 60 -i input.mp4 -qscale:v 4 -frames:v 1 output.jpg

Add Text subtitles to a video using FFmpeg#

ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4

The order of -c copy -c:s mov_text is quite important.

You are telling FFmpeg to do the following:

  1. Video: copy, Audio: copy, Subtitle: copy
  2. Subtitle: mov_text

Alternatively, you could just use -c:v copy -c:a copy -c:s mov_text in any order.
This method adds the subtitles to your video file as one of the streams. You will need player support to view the subtitles (such as VLC).

Burn subtitles to a video using FFmpeg#

This will “burn them into” the video, meaning you can’t turn them off in the player. This is different from adding them as a subtitle stream which can be read by the player and displayed if the viewer wants them.

If your FFmpeg has libass enabled at compile-time, you can directly do:

ffmpeg -i mymovie.mp4 -vf subtitles=subtitles.srt mysubtitledmovie.mp4

Otherwise, you can the libass library (make sure your FFmpeg install has the library in the configuration --enable-libass).

First, convert the subtitles to .ass format: (haha fun extension name)

ffmpeg -i subtitles.srt subtitles.ass

Then add them using a video filter:

ffmpeg -i mymovie.mp4 -vf ass=subtitles.ass mysubtitledmovie.mp4

Extract duration from FFmpeg video output#

ffmpeg -i file.mp4 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g'

I hope you enjoy some of these. I’ll take some more time out to explain all of these and add a couple more which are tied up in a bit extra complex workflow, un-needed for this post.

Use your code for good. Peace!

Helping businesses understand developers. Head of Developer Relations RapidAPI.

๐Ÿ“Ÿ Edutainer at Node.js CLI Automation course (100 videos ยท 22 Projects) & VSCode.pro course (over 20K devs). ๐ŸŽฉ Award-winning Open Source Engineer & Dev Advocate ๐ŸฆŠ Google Developers Expert Web DevRel ๐Ÿš€ Mars Ingenuity Helicopter code contributor ๐Ÿ† 8th GitHub Stars Award recipient ๐ŸŒณ Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator.

โœŒ๏ธ Author of various open-source dev-tools and software libraries used by millions of developers worldwide โ“ฆ WordPress Core Developer ๐Ÿ“ฃ TEDx Speaker with 100+ international talks.

๐Ÿ”ฅ Satya Nadella ยท CEO of Microsoft, said โ€” Awais is an awesome example for developers.
๐Ÿ™Œ Leading developers and publishing technical content for over a decade ๐Ÿ’œ Loves his wife (Maedah) โฏ Read more about Ahmad Awais.

๐Ÿ‘‹โ€ฆ Awais is mostly active on Twitter @MrAhmadAwais

๐Ÿ“จ

Developers Takeaway

Takeaway my professional opinion on open-source, developer relations, growing your business with family, and everything in between! Delivered to your inbox a couple of times every year. I'm even funny at times. I hate spam โ€” pinky-promise!

๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป 118,947 Developers Already Subscribed
Comments 0
There are currently no comments.