Streaming video

From EChase
Jump to: navigation, search

Whilst AVI and XviD are great for working with and processing, they're no good for streaming as the AVI container needs to be complete for the video to play (in everything apart from mplayer, anyway). As such, we have to convert it to a format that can be streamed easily.

There are number of different formats available for streaming video on the Internet, but it breaks down like this: Real Media is great and really versatile but costs a bomb to licence, Flash has the highest player install base but is proprietary and costs lots too, Windows Media comes free with Windows but is proprietary and not supported by default on Mac, and MP4 can be streamed using open-source software but needs multiple files to support multiple bandwidths.

In short, we're going to use MP4 streamed through Apple Darwin Streaming Server unless someone gives us reason to do otherwise. MP4 can be played in Apple Quicktime and Real Media player, but not (currently) Windows Media Player. But surely all Windows users have iTunes now, right? And even if they don't, what's a 32MB download between friends?

So how do we get from AVI or whatever to MP4. Well, one way is this...

VID2MP4.bat[edit]

Creating MP4s can be a bit of a PITA. Not only do you have to convert the video stream, you also have to convert the audio stream too. This means you have to demux the input file and uncompress the audio stream to PCM (WAV) and then recompress using AAC and transcode the video stream to MPEG4. Then, once that's done, you need to add a 'hint' track to the MP4 so that the server can optimise the streaming of it (don't ask what it optimises, I don't know or particularly care).

@echo off

:: Usage: vid2mp4 <infile> <outfilename> <fps>

setlocal

echo.
echo vid2mp4 %1 %2 %3
date /T 
time /T


set FPS=25
if not %3 == "" set FPS=%3

:: Copy source video to ensure filename has no spaces (tools don't like spaces)

:: Create temp directory to work in
if not exist tmp mkdir tmp

echo Copying source video to temporary file...
if not exist tmp\temp%~x1 copy %1 tmp\temp%~x1 >nul

if %~x1 == .wmv (
	:: demuxer doesn't like WMV, so convert to AVI
	if not exist tmp\temp.avi call wmv2avi tmp\temp%~x1 tmp\temp.avi %FPS%
)

:: Demux audio from input video
echo Demuxing audio...
if not exist tmp\temp.wav ffmpeg -i tmp/temp%~x1 -vn tmp/temp.wav

:: Encode audio to AAC
echo Encoding audio...
if not exist tmp\temp.aac faac --mpeg-vers 4 tmp/temp.wav -o tmp/temp.aac

:: Demux video from input AVI
echo Demuxing video...
if not exist tmp\temp.m4v ffmpeg -i tmp/temp.avi -an -r %FPS% -sameq -trell tmp/temp.m4v

echo Encoding MP4...

if exist %2.mp4 del /Q %2.mp4

:: Encode M4V video to MP4
echo Muxing in video stream...
mp4creator -r=%FPS% -create=tmp/temp.m4v -hint %2.mp4

:: Mux AAC audio into MP4
echo Muxing in audio stream...
mp4creator -create=tmp/temp.aac -hint -interleave %2.mp4

:: Optimize MP4 for streaming
echo Optimizing...
mp4creator -optimize %2.mp4

REM echo Cleaning up...
REM Clean up temp files
REM del /Q tmp\*.*

REM Clean up temp directory
REM rmdir tmp

echo Done.

date /T 
time /T

:EXIT

Again, the script is fairly simple, so I'll just give the usage:

vid2mp4 collections/orf/EasternEurope1.avi "/Program Files/Darwin Streaming Server/Movies/collections/orf/EasternEurope"

Note that the output file name doesn't include the file extension - this is because it's added automatically by the script.

To test the output MP4 file, just use the Quicktime or Real Media player, or mplayer.

A note about mp4creator on win32[edit]

I've found that there appears to be a bug in the current win32 binary of mp4creator. When creating an MP4, it sometimes crashes but still succeeds in creating the MP4. This is somewhat annoying as the crash halts any script that is running, meaning that the videos must be converted manually. The pre-built win32 binary is quite old - using a newer version (i.e., building it yourself) may fix the problem.

Back to video tips and tricks