Friday, November 30, 2018

Some FFMPEG Commands I Find Useful

This post is more for me than for you. I've been working with FFMPEG and AVCONV quite a bit recently, and I've had to reuse these commands quite a bit over that time. I'm putting them here so I don't lose them, and I also hope that someone finds them useful at some point in the future.  So, without further ado, here are some FFMPEG things I do a lot.


Making a Video out of an Image and Audio

I use this one if I want to archive audio on YouTube. I create an image (generally with ImageMagick) and then I run this FFMPEG command (can be translated to AVCONV by simply replacing the command, as most of these can.)

ffmpeg -loop 1 -i image.jpg -i audio.wav -c:v libx264 -tune stillimage -c:a aac -strict experimental -b:a 192k -pix_fmt yuv420p -shortest out.mp4

Where image.jpg is the image, audio.wav is the audio (it doesn't have to be WAV), and out.mp4 is where it's going.

Concatenate Things

ls -1 *.mp4 | awk '{print "file \47"$1"\47"}' > concat.txt
ffmpeg -f concat -i concat.txt -safe 0 -c copy out.mp4

Prints all mp4 files as a concat list then concatenate them without transcoding. I find this especially useful when my camera breaks large files up into smaller recordings because of filesystem limitations.

Recursively Transcode Files

It turns out I transcode a lot of media. Mainly I just try to convert things into HEVC format so it takes less space. But usually I want to do it in a folder automatically. So I use the following command:

find . -iname '*.avi' -exec sh -c 'ffmpeg -i "$1" -c:v libx265 -preset medium -crf 22 "${1%.avi}.mkv"' sh {} \;

This will find all AVI files and put them through FFMPEG and they'll come out renamed with an MKV extension on the other end. Then I can recursively delete all AVI files. But I already hear you asking: what do I do if the source files are MKV files too? Easy.

find . -iname '*.mkv' -exec sh -c 'ffmpeg -i "$1" -c:v libx265 -preset medium -crf 22 "${1%.mkv}.mkv"' sh {} \;

And then you can delete them with

shopt -s extglob
rm ./**/!(*265.mkv)

Which removes all files that do not end with "265.mkv".

Create a Timelapse From Sequenced Photos

I usually make timelapses with my GoPro camera, and I take enough pictures at a reasonable enough rate that I can usually make 60fps timelapses. I can do it at 4k, but that doesn't mean my computer always enjoys playing it back at full quality. Here's a 60fps example at 1080p:

ffmpeg -r 60 -start_number 1 -i GOPR%04d.JPG -s 1920x1080 -vcodec libx264 timelapse.mp4

This should create a decent quality starting from picture #1. You can change the start number to whatever you need it to be.

That's about all I have for this time. I hope to do something with either Calvin or the laptop. Maybe some day. Who knows? I'm sick and tired of writing filler pieces.

No comments:

Post a Comment