FFMPEG Commands

From Unixcat.net Wiki
Jump to navigation Jump to search

Here are some FFMPEG scripts I've come up with for various things, mostly converting media of one format to another. Many of the scripts are working on the Bourne Shell on FreeBSD, since that's where I'm converting most of my media at the moment. They haven't been tested in Bash, but should work.

You should have some familiarity with FFMPEG and shell scripting. These are provided as a convenience; use at your own risk and be sure to make backups. I am not responsible for any loss of data as a result of using these!

Video

Converting video from one format or container to another. Lines that look like this are meant to just be run as a single command at the terminal:

$ ffmpeg -i infile.avi outfile.mp4

Whereas something meant to run as a script will look like this:

#!/bin/sh

ffmpeg -i infile.avi outfile.mp4

The above should go in a text file and be made executable.

Unless otherwise noted, the scripts are meant to be run in a directory full of files you want to convert. You can put the script someplace like ~/bin, change to the directory, and just run it. If you have a lot of files something like screen or tmux is recommended.

Convert Video to x264

This script is useful if you have a device that's older and just plays x264-encoded files. You probably need libx264 installed.

x264conv.sh

#!/bin/sh

for i in *.mp4 *.mkv
do
        j=$(echo $i | sed 's/x265/x264/g')
        ffmpeg -i "$i" -acodec copy -vcodec h264 "$j"
done

This assumes that the file has x265 in the name. It writes a new file with x265 changed to x264, which is reencoded to x264. You'll have to go in and delete the x265 files if you don't want to save them. You should probably also check that the new x264 files play correctly.

Convert 10 Bit HEVC x265 to 8 Bit x264

Some devices don't like 10 bit HEVC x265 files. This converts the files to x264.

10to8x264.sh

#!/bin/sh

for i in *.mp4 *.mkv
do
        j=$(echo $i | sed 's/x265/x264/g')
        ffmpeg -i "$i" -map 0 -c:v libx264 -crf 18 -vf format=yuv420p -c:a copy "$j"
done

See this SuperUser page for reference. As before, x265 is renamed to x264 for the new file. Run this in a directory and delete the old files if desired, after verifying the new ones play.