Create a HLS4 With AES Stream

Edit: Found out that ffmpeg could do the segmenting instead of segmenter

Here’s a short tutorial on how to create a Http Live Stream v4 with AES encryption from a MOV file with multiple language track. Here are the list of softwares that we’ll be using for this tutorial:

If you’re on Windows:

Converting the MOV file to TS file

  1. Convert the MOV file to MP4. HandBrakeCLI -i -o VIDEO_FILE_NAME.mp4 --preset=iPad -d slow
  2. Convert the MP4 file to segments of TS file. ffmpeg -i VIDEO_FILE_NAME.mp4 -acodec libfaac -vcodec libx264 -an -map 0 -f segment -segment_time 10 -segment_list segment.m3u8 -segment_format mpegts -vbsf h264_mp4toannexb -flags -global_header stream-%d.ts
  3. Use OpenSSL to create a random key file. openssl rand 16 > static.key
  4. Use hexdump on the static.key to retrieve the hex key.
  5. For each TS file generated by the segmenter command, use openSSL to encrypt the TS files. openssl aes-128-cbc -e -in stream-1.ts -out tsAes\stream-1.ts -p -nosalt -iv 0 -K YOUR_AES_KEY. Your encrypted TS files should be in the tsAes folder.
  6. Move the static.key to the tsAes folder.

Extracting the audio tracks from the MOV file

You’ll have to know which audio track belongs to which language. The easiest way to check the language is to play the video via VLC and identify the language of each audio track. Let’s assume that Stream 0:1 is English and Stream 0:2 is Chinese.

  1. Extract the English audio from the MOV file. ffmpeg -i -map 0:1 -ac:a:0 2 -acodec libfaac -vn track_en.aac
  2. Extract the Chinese audio from the MOV file. ffmpeg -i -map 0:2 -ac:a:0 2 -acodec libfaac -vn track_zh.aac
  3. Go to trackEn folder and run ffmpeg -i track_en.aac -map 0:1 -ac:a:0 2 -acodec libfaac -vcodec copy -dcodec copy -vn -f segment -segment_time 10 -segment_list segment.m3u8 -segment_format mpegts -vbsf h264_mp4toannexb -flags -global_header stream-%d.ts
  4. Go to trackZh folder and run track_zh.aac file to TS files. ffmpeg -i track_zh.aac -map 0:2 -ac:a:0 2 -acodec libfaac -vcodec copy -vn -f segment -segment_time 10 -segment_list segment.m3u8 -segment_format mpegts -vbsf h264_mp4toannexb -flags -global_header stream-%d.ts

Edit: We’ll need to do segmenting and the audio extraction in one line to preserve the PTS values. Incorrect PTS values will result in audio/video out of sync.

So finally, you should have the following directory structure.

  • root
    • tsAes
      • all your encrypted video files
      • static.key
      • stream.m3u8
    • trackEn
      • all your English audio track segments
      • stream.m3u8
    • trackZh
      • all your Chinese audio track segments
      • stream.m3u8

On the root directory, create a stream.m3u8 file. It should look something like this:



Copyright © 2016 - Benson Lim -