On my work, technology and related stuff....

Posts Tagged ‘handbrake’

22 comments

iOS devices support the delivery of multimedia content via HTTP progressive download or HTTP Live streaming . As per Apple's guidelines to App developers, "If your app delivers video over cellular networks, and the video exceeds either 10 minutes duration or 5 MB of data in a five minute period, you are required to use HTTP Live Streaming. (Progressive download may be used for smaller clips.)". 

This article discusses a method for generating HTTP live streaming content using freely available tools. If your needs are large scale, then you may need to explore commercial encoders such as the Sorenson Squeeze 7 or other commercial video platforms. This article does not describe commercial video platforms. 

HTTP Live Streaming – A (Very) Brief Overview:

 In HTTP Live streaming, the multimedia stream is segmented into continuous media segments wherein each media chunk/segment holds enough information that would enable decoding of the segment. A playlist file is a list of media URIs, with each URI pointing to a media segment. The media URI’s are specified in the order of playback and the duration of the playlist file is the sum of the durations of the segments. Media playlist files have an “.m3u8” extension. The servers host the media segments and the playlist file. In order to playback content, the media streaming app on the device fetches the playlist file and the media segments based on the media URIs specified in the playlist . The transport protocol is HTTP.
Now, you can have multiple encodings/renditions of the same multimedia content. In this case, you can specify a variant playlist file that will contain URIs to the playlist files corresponding to each rendition of content. In this case, the iDevice can switch between the various encodings thereby adapting to changing network bandwidth conditions. HTTP Live Streaming is essentially a form of adaptive streaming. You can get more details from the IETF I-D available here. Other well-known adaptive streaming protocols include that of Microsoft’s Smooth Streaming , Adobe’s Dynamic Streaming  and the DASH standards specification .

Encoding the content using Handbrake:

  1. Among the free tools, I’ve found Handbrake to be the best in terms of performance and supported formats. Versions are available for Windows and the Mac. In my experience, the Mac version stalled a few times during encoding and at times hogging all the CPU cores on my Macbook Pro. The Windows version worked flawlessly.

  2.  Use the following encoding guidelines provided by Apple to encode your content. If you expect your app users to be able to access the content under a variety of network conditions (wifi, cellular etc), you would want to support multiple encodings of the content .

 
 
 

The Tools for generating content for HTTP Live Streaming :

Once you have encoded your content, you would have to prepare it for delivery via HTTP live streaming. There are a command line utilities available for the Mac that can be downloaded for free from  http://connect.apple.com/ (You would need an Apple developer Id for installing the tools, which again is free). You would need the following tools –

  • mediafilesegmenter

  • variantplaylistcreator

Once installed, they would be available in/usr/bin directory of your Mac.

 Segmenting your encoded content:

Use the mediafilesegmenter tool to segment the encoded media files.  You would need to be “root” user in order to run the tool.
 
/usr/bin/mediafilesegmenter [-b | -base-url <url>]
                        [-t | -target-duration duration]
                        [-f | -file-base path] [-i | -index-file fileName]
                        [-I | -generate-variant-plist]
                        [-B | -base-media-file-name name] [-v | -version]
                        [-k | -encrypt-key file-or-path]
                        [-K | -encrypt-key-url <url>]
                        [-J | -encrypt-iv [random | sequence]]
                        [-key-rotation-period period]
                        [-n | -base-encrypt-key-name name]
                        [-encrypt-rotate-iv-mbytes numberMBytes]
                        [-l | -log-file file] [-F | -meta-file file]
                        [-y | -meta-type [picture | text | id3]]
                        [-M | -meta-macro-file file]
                        [-x | -floating-point-duration] [-q | -quiet]
                        [-a | -audio-only] [-V | -validate-files] [file]
 

 

  • Open a terminal window on your Mac.
  • Type “man mediafilesegmenter”at the command link prompt to get a full description of the usage of the tool.

          <command prompt>$ man mediafilesegmenter
 

  • An example of using the tool to segment a media file named “mymedia_hi.mp4”  is as follows-

        <command prompt>$ sudo /usr/bin/mediafilesegmenter -I -f mymedia_hi -f mymedia_hi.mp4
You will be prompted for the root password (which you must provide)
 
In the example, the media file “mymedia_hi.mp4” is assumed to be present in the current directory from which the command is executed. Otherwise, be sure to specify the path to the media file. The segments will be generated in a subfolder named “mymedia_hi” within the current directory.
 
<command prompt>$ cd mymedia_hi
<command prompt>$ ls
<command prompt>$
fileSequence0.ts           fileSequence14.ts         fileSequence6.ts
fileSequence1.ts           fileSequence15.ts         fileSequence7.ts
fileSequence10.ts         fileSequence2.ts           fileSequence8.ts
fileSequence11.ts         fileSequence3.ts           fileSequence9.ts
fileSequence12.ts         fileSequence4.ts           prog_index.m3u8
fileSequence13.ts         fileSequence5.ts

 
The fileSequence*.ts files correspond to the media segments which are MEPG2 transport streams. The prog_index.m3u8 is the playlist file corresponding to the segmented media file and specifies the media URIs to the segments.
 

  • The “-I” option that I specified in the example command will generate a variant plist file.  The variant plist file would be subsequently required to generate the variant play list file  as described in the next step. You don’t need this option if you don’t plan on streaming multiple encodings of your content.

         Assuming you are in the “mymedia_hi” folder, type the following to get the list of generated variant plist files.
 
<command prompt>$ cd ..
<command prompt>$ ls *.plist
<command prompt>$
mymedia_hi.plist

 

Generating variant playlist file :


If you do not intend to stream multiple renditions of the content , you can skip this step and proceed to the “Streaming the Content” section.

  • Follow the procedures specified in “Segmenting your encoded content” section to segment every encoding of the media content that you wish to stream.  For example, if you plan on supporting three renditions of your media content for high, medium and low network bandwidth conditions respectively, you will have to use the “mediafilesegmenter” tool to segment each of those renditions.

  • Use the tool variantplaylistcreator for generating the variant playlist file.

    /usr/bin/ variantplaylistcreator [-c | -codecs-tags] [-o | -output-file fileName]
    [-v | -version] [<url> <variant.plist> ...]

 

  • Type “man variantplaylistcreator” at the command link prompt to get a full description of the usage of the tool.

    <command prompt>$ man variantplaylistcreator
 

  • An example of using the tool to generate a variant playlist file named “mymedia_all.m3u8”  is as follows-

<command prompt>$ sudo /usr/bin/variantplaylistcreator -o mymedia_all.m3u8 http://mywebserver/mymedia_lo/prog_index.m3u8 mymedia_lo.plist http://mywebserver/mymedia_med/prog_index.m3u8 mymedia_med.plist http://mywebserver/mymedia_hi/prog_index.m3u8 mymedia_hi.plist
 

You will be prompted for the root password (which you must provide)
 
The URL that is associated with the prog_index.m3u8 files for each encoding corresponds to the URL of the webserver that will be used for hosting/streaming the media content.
 

  • The generated variant play list file “mymedia_all.m3u8” will specify the URIs to playlist files (prog_index.m3u8) corresponding to each encoding of the media file. The contents of the file (viewable using your favorite text editor) should be something like this 

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=87872
http://mywebserver/mymedia_lo/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=100330
http://mywebserver/mymedia_med/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1033895
http://mywebserver/mymedia_hi/prog_index.m3u8

 

Streaming the Content:

  1. Upload the variant playlist file (mymedia_all.m3u8 in our example) and ALL the sub-folders that contain the generated segments for every rendition of the media file to the web server that will stream the content. You do not have to copy the media files (eg .mp4 files) from which the segments were generated. You could host it on any regular web server such as the Apache Webserver or Microsoft's IIS. For instance, on an Apache server on Windows, you would copy all this to the “htdocs” folder. This would be something like this-  c:\Program Files(x86)\Apache Software Foundation\Apache2.2\htdocs.

  2. Typically you would need to make no changes to the web server in order to stream the content. In some cases, you may need to update the  webserver configuration file to add support for the .m3u8 MIME type . I had to do this for my IIS web server where I associated the .m3u8 extension with “application/octet-stream” MIME type. 

Note: If your needs are large scale, you would employ the services of a Content Distribution Network (CDN) such as Akamai to publish and distribute your content.

Accessing the Content from your iDevice:

  1. In order to access the streaming content on your iDevice, the media URL must point to the appropriate playlist (.m3u8) file. This would correspond to either the Variant playlist file if you support multiple encodings of the content (mymedia_all.m3u8 in our example) or the prog_index.m3u8 playlist file for the specific encoding of the content.

Example: http://<webserver>/<path to the .m3u8 playlist file>
 
If you do not have a streaming app, you can open the URL in the Safari browser on your device. If everything goes well, the stream should start playing on your device.