Brain Dump

A place to store my random thoughts and anything else I might find useful.

Posts Tagged ‘cli’

Transcoding Video for Android

Posted by mzanfardino on September 17, 2013

Contents

1. Overview
2. Solution
3. Summary

Overview

Android devices have a limited number of video and audio codecs that are supported natively. I have a collection of videos that I’d like to be able to watch on my Nexus 10, but simply uploading the files does not always work. Many of the files are encoded in mkv or avi containers with mp3 or ac3 audio encoding. These will not play with the native video player and attempts to install a full-featured player such as vlc have been frustrating. The solution is to transcode the video to mp4 encoding with aac audio encoding. Note: there are other compatible video and audio codecs such as 3gpp, but I have found that mp4 and aac are most straight-forward codecs to transcode.
top

Solution

Transcoding can be performed with a number of different tools and a number of different approaches may well be valid. I am documenting here the solution that works for me. I have been using ffmpeg for years to transcode, so naturally this is the tool I turned to. Assuming all the appropriate libraries have been installed, you can use ffmpeg to transcode from mkv or avi with a few simple switches.

The first issue I wanted to address was the size of the files. A number of my files are in excess of 1GB. This is due to the frame-size of the videos: some as large as 1280×720. I clearly don’t need that high resolution for playback on my Nexus 10, so reducing the frame-size to something more manageable has the desired effect of reducing the file size. I prefer video scaled to 720xW (where W is relative to the original scale dimension). Why this particular frame-size? It’s high-enough resolution to maintain a decent quality video; it’s small enough that most videos will down-size to this frame-size (rather than forcing an up-size which can be ugly); and maintains the correct aspect ratio. You can of course substitute any frame-size you like.

The second issue I need to address is audio codecs. Many of the videos I have are encoded using ac3 which is not supported. I chose aac as my audio codec as this is supported by the native video player and it will preserve 5.1 if the source is encoded as such.

The following is the general form of the command I use:

$ ffmpeg -i foo.mkv -c:v libx264 -preset slow -crf 22 -vf scale=[1920,1280,720]:-1 -c:a aac -strict -2 foo.mp4

I wanted to transcode both avi and mkv in one pass. I also wanted to do more than one file at a time, so I constructed the following command to transcode a block of files:

$ for f in *.{avi,mkv}; do ffmpeg -i ${f} -c:v libx264 -preset slow -crf 22 -vf scale=[1920,1280,720]:-1 -c:a aac -strict -2 ${f/${f: -3}/mp4}; done;

NOTE: in the above the ‘scale’ parameter should be only one of the three suggested values. By setting the width to -1 the resulting video will be scaled correctly. One caveat: in some instances the width value returned from -1 will be an odd number which will cause ffmpeg to throw an error. In that case I mere adjust the width to the next highest even integer and rerun the command providing the calculated width rather than the -1.

There are a number of consideration when performs transcoding that I have not touched on. I have tried to address the most common situations I’ve run into relating to: video and audio codec as well as maintaining aspect ratio.

The final step is to transfer the newly transcoded videos to my Nexus. This was done with a simple adb push command (no need to root access).
top

Summary

Android is a wonderful platform with a number of fantastic features. However, given the limited set of video and audio codecs supported, it’s necessary to convert your videos you might otherwise be able to “just play” on your PC. The process I’ve outlined is just one way to achieve this goal. With linux and adb it can be a very easy process to convert your videos to a playable format (while reducing the file size) and move them to your favorite android device.
top

Advertisements

Posted in android, ffmpeg, linux, transcode, video | Tagged: , , , , | Leave a Comment »