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!

Founder & CEO at Langbase.com ยท Ex VP DevRel Rapid ยท Google Developers Advisory Board (gDAB) founding member. ๐Ÿง‘โ€๐Ÿ’ป AI/ML/DevTools Angel Investor โฏ AI/ML Advisory Board member San Francisco, DevNetwork

๐ŸŽฉ Award-winning Open Source Engineer & Dev Advocate ๐ŸฆŠ Google Developers Expert Web DevRel ๐Ÿš€ NASA Mars Ingenuity Helicopter mission code contributor ๐Ÿ† 8th GitHub Stars Award recipient with 3x GitHub Stars Award (Listed as GitHub's #1 JavaScript trending developer).

๐ŸŒณ Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator. ๐Ÿ“Ÿ Teaching thousands of developers Node.js CLI Automation (100 videos ยท 22 Projects) & VSCode.pro course. Over 142 Million views, 22 yrs Blogging, 56K developers learning, 200+ FOSS.

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

โœจ As quoted by: Satya Nadella ยท CEO of Microsoft โ€” 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

Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!

โœจ 172,438 Developers Already Subscribed
Comments 0
There are currently no comments.