Converting TWiT Video

From The Official TWiT Wiki
Jump to: navigation, search
Info.png Information on this page needs to be updated. BitGravity is no longer a streaming provider.

This page describes the process of converting a stream captured from BitGravity or Flosoft to an MP4 or AVI (or another container of your choosing) using free and open source software.

Setting Up For The First Time

The three pieces of software we'll need for this process are ffmpeg (0.5 or later), MP4Box and FAAC.

There have guides for installing wget, curl, ffmpeg and mp4box on the following operating systems:

Capturing The Stream

There are several methods of capturing the stream. You can learn how to capture the stream and install the necessary programs in detail at How to Record TWiT.

Converting To An MP4 using ffmpeg

We now want to convert this flv to an mp4 so we can distribute it and use it on a greater variety of video players. A neat thing we can do is convert this flv to an mp4 without losing a single bit of quality on the video because they both use h.264. We will however need to transcode the audio from mp3 to aac or the finished mp4 will not work on some video players (including Quicktime). To start converting the video you need to issue the command:

ffmpeg -i output.flv -vcodec copy -acodec libfaac -ac 1 -ab 64k output.mp4

Lets take a look at what this command does. You first have a flag called -i output.flv which tells ffmpeg that output.flv is the file we want to convert from. -vcodec copy tells ffmpeg that we want to use the video as is, with no transcoding. -acodec libfaac tells ffmpeg that we want the audio to be transcoded to aac using the FAAC encoder. -ac 1 means that we only want a single audio channel (ie: mono sound). TWiT only sends the left channel to BitGravity so we want to have that single channel outputted to both the left and right speakers. -ab 64k simply means that we want the bitrate of the aac to be 64kbps. The final parameter specifies the name of the output file, output.mp4.

After that command is finished you will have a new file named output.mp4 which should play on most media players.

More information on ffmpeg can be found here.

iPhone and iPod compatibility; also, Converting to other bit rates

This mp4 will not play on an iPod or iPhone because of the way the h.264 is encoded by the BitGravity software. You will need to transcode the video using the x264 encoder in order for that to work. This too can be done with ffmpeg.

Starting with the output.mp4 file from the previous step, one could use the following command:

ffmpeg -i output.mp4 -f mp4 -vcodec mpeg4 -maxrate 250000 -b 225000 -qmin 3 -qmax 5 -bufsize 16384 -g 300 -acodec libfaac -ab 64k -ac 1 -s 480x320 -dts_delta_threshold 1000 -aspect 16:9 output-iphone.mp4

The resulting file output-iphone.mp4 is sized for the iPhone (the "-s 480x320" and "-aspect 16:9" result in 480x270 video within the iPhone's 480x320 screen) and uses standard h.264 format, so should play on any h.264-compatible player. It will also be much smaller than the original captured .flv or the output.mp4 file, as it uses an average video bit rate of only 225 kbits/second with a maximum peak of 250 kbits/sec. This will result in a file about one fourth the size of the previous file (assuming the .flv was captured from the BitGravity high-quality stream). There will of course be a loss of quality, but that is usually acceptable on a small screen like the iPhone's.

These particular parameters are very similar to those used to encode the video files offered by Odtv. Other output frame sizes and bit rates can be tried for different players and situations.

n.b.: The two ffmpeg steps cannot be combined into one, logical though that might seem.

Converting to an MP4 in Windows

For this method you will need an FLV file you captured using either wget or curl. When you are done you will end up with an mp4 file that can be played on any player or edited. Download and install FLV Extract and Yamb.

  • Start FLV Extract. Use FLVExtract.exe, FLVExtractCL.exe is the CLI version. When FLV Extract starts a small window will appear.
  • Drag the FLV file you want to convert onto this window. FLV Extract will extract 2 files, a .264 and a .aac file, into the same folder as the FLV file. When the extraction is done click Ok.
  • Start Yamb. Double click the selection labeled "Click to create an MP4 file ...". It says click but you have to double click it.
  • Drag the .264 and .aac files you created earlier into the window that appears and click Next.
  • Yamb will create a .mp4 file from the 2 files. When it's done click finish.

Converting the MP4 to an AVI

This method will allow you to convert the MP4s you created earlier to AVIs using an OSS program named MP4Cam2AVI. You can also concatenate 2 or more MP4s into an AVI. Download and install MP4Cam2AVI.

  • Start MP4Cam2AVI and load the MP4 file or files you want to convert with either the File menu or the file selection box on the left. MP4Cam2AVI can do batch converts or concatenate MP4s.
  • Select any settings you need such as output file name, audio format, action, etc. and select Start.
  • MP4Cam2AVI will produce 1 or more AVI files depending on your settings.

Attaching Two MP4s (optional)

If you want to attach two mp4 files together there's a fairly simple way of doing so. You might want to do this if you're attaching bumpers to the show you just downloaded or if there was a network problem and the show was downloaded in multiple files. For this example we will be joining output.mp4 to outputpart2'mp4 and saving it as finaloutput.mp4. First we need to separate the video and audio from both files using MP4Box. Before you start keep in mind that the audio in the mp4 needs to be the same format and bitrate. The video also needs to be the same format, bitrate and framerate. Issue the following commands to separate the video from the audio:

$ MP4Box -raw 1 -v output.mp4
$ MP4Box -raw 2 -v output.mp4
$ MP4Box -raw 1 -v outputpart2.mp4
$ MP4Box -raw 2 -v outputpart2.mp4

You will now have four new files; output_track1.h264, output_track2.aac, outputpart2_track1.h264 and outputpart2_track2.aac. You will now want to concatenate the video files to one another and the audio files to one another.

On Windows:

C:\> copy output_track1.h264 + outputpart2_track1.h264 concatvideo.h264
C:\> copy output_track2.aac + outputpart2_track2.aac concataudio.aac

On *NIX:

$ cat output_track1.h264 outputpart2_track1.h264 > concatvideo.h264
$ cat output_track2.aac outputpart2_track2.aac > concataudio.aac

You will then have two new files; concatvideo.h264 and concataudio.aac. These have all of the video and audio information of each of the original files combined. The last step is to put the video and audio back into an mp4 container with the following command:

$ MP4Box -add "concatvideo.h264:fps=29.970" -add "concataudio.aac" -v finaloutput.mp4
-substitute 29.970 for the framerate of the video such as 30.000 for 30 frames per second

You should now have a file named finaloutput.mp4 which seamlessly combines both of the videos you had at the beginning of this process.

Cutting an MP4 File (optional)

You may also need to cut or trim an MP4 file if you started capturing too early or didn't stop when the end of the netcast ended. This is accomplished by using MP4Box from GPAC. To cut or trim the MP4 file, issue the following command:

$ MP4Box -splitx 1:220 -v output.mp4

This outputs an MP4 file named output_1_220.mp4. The 1 here corresponds to when the cut video should start and the 220 here says where the cut video should end. The 1 and 220 mean seconds. So the length of the cut video is 1 minute and 59 seconds or 119 seconds. You will need to change these values to what is appropriate for the MP4 file you are cutting. If you specify 0 for the start, then it starts at the beginning. You cannot specify a higher value then the length of the original MP4 file.

Converting To An OGV

Instead of converting from flv to mp4, you can convert to ogv (OGG Video). This consists of the OGG Theora video codec, and the OGG Vorbis audio codec. To do this, simply compile ffmpeg with x264 support (using the instructions above, under Getting Started) and run the command:

$ ffmpeg -i output.flv -vcodec libtheora -acodec libvorbis -aspect 16:9 output.ogv