Convert all AVIs in your video library to MP4

I have a large video library and I’ve been on the look out for the best device to access all this media. It must support DLNA, not have cinavia, and obviously I’d like it to support as many audio and video codecs as possible. That eliminates most Sony products because they all seem to have Cinavia including PlayStation. I tried a chromecast and I won’t go into the details of how much I absolutely hated that useless piece of garbage. I still have a device running GoogleTV which is definitely my favorite, but unfortunately it has been discontinued by Google.

After much research I bought a Roku. I like it a lot, but it can be pretty picky about audio and video codecs. When videos have multiple audio streams whether it be DTS and stereo or multiple languages, the device will sometimes have no audio or play the wrong language. Fortunately, it is generally pretty simple to demux the streams and remap them in a way that the Roku will tolerate, but the device does not support AVI. This means if I want to keep the Roku around, I’ve either got to run Plex or some other transcoding capable DLNA server or convert all of my AVIs to H264 MP4s. I like to try to be as efficient as possible so which rules out transcoding a video every time you watch it, so I developed a little bash script to find all AVI files in my video library to MP4.

To run the script, you’ll need to have the perl-based “rename” utility installed as well as ffmpeg.

find /path/to/your/video/library/ -name "*.avi" -exec ffmpeg -i '{}' -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 '{}'.mp4 \; -exec rename 's/.avi.mp4/.mp4/' "{}.mp4" \; -exec rm -f '{}' \;

Just change “/path/to/your/video/library/” to the real path to your video library and let the script do its thing. If you’d like to convert other video types, just change the search parameters “-name *.avi” to something that suits your needs. All videos will be re-encoded to H264 video, and 192k stereo AAC audio. It will then rename the file and delete the original file.

If anyone has any modifications or useful custom scripts you’d like to share, please leave them in the comments.

Update:

find /path/to/your/video/library/ -name "*.avi" -exec ffmpeg -i '{}' -c:v libx264 -crf 19 -preset slow -strict -2 -c:a aac -b:a 192k -ac 2 '{}'.mp4 \; -exec sh -c 'mv "$0.mp4" "${0%.avi}.mp4"' '{}' \; -exec rm -f '{}' \;

This one-liner doesn’t depend on a specific version of the rename utlilty. It also supports more versions of ffmpeg. The only flaw now is it only supports lowercase avi extension. Still working out the rename part of the script to handle that properly.

Leave a Reply

Your email address will not be published. Required fields are marked *