HLS(http live streaming) m3u8自制

转载 2015年07月08日 11:55:25

Apple HTTP Live Streaming (HLS) has been a nightmare to get working. Below, I’ll go through some of my trials and tribulations in getting HLS encoding for non-live streams working in Windows.   In summary, I couldn’t get the bitrate I wanted on my videos.  I’ll provide explanations (and rants) below, but first, for the impatient, here is the solution.

In the end, here is exactly what I did:

  • Download FFMPEG, latest stable release (0.6.1 in my case) Windows build from VideoHelp.com(FFMPEG.org doesn’t do their own builds for some reason?)
  • Download x264 Windows build from x264.nl (once again,the x264 team doesn’t do their own windows builds)
  • Download the Nero AAC Encoder from nero (this is NOT open source)
  • Download the open source segmenter from here (author’s site is here)

Here’s how I have created my streams in a Windows environment:

  1. Extract WAV file from original video:
    ffmpeg.exe -i INPUT.AVI -vn -acodec pcm_s16le AUDIO.WAV
  2. Produce an AAC file:
    neroAacEnc.exe -cbr 64000 -he -if AUDIO.WAV -of AUDIO.AAC
  3. Produce the required audio-only MPEG2-Transport Stream:
    ffmpeg.exe -i AUDIO.AAC -vn -f mpegts -acodec copy AUDIO.TS
  4. Go ahead and segment the audio-only MPEG2-Transport Stream (this can be done later):
    segmenter.exe AUDIO.TS 10 STREAMNAME/AUDIO STREAMNAME/AUDIO.M3U8 http://mydomain.com/virtual/path/to/STREAMNAME/
    Note that this will dump AUDIO-*.TS and AUDIO.M3U8 files into the .\STREAMNAME directory which must exist prior to running.
  5. Choose a bitrate, and encode the video for one of those bitrates. Apple recommends 96 kb/s, 256 kb/s, and 800 kb/s for video. This example will use 96 kb/s (“low” bitrate):
    x264.exe --level 30 --profile baseline --bitrate 96 --keyint 30 -o LOW.MP4 INPUT.AVI
  6. Mux the audio and video together to produce an MPEG2 Transport Stream:
    ffmpeg.exe -i LOW.MP4 -i AUDIO.AAC -f mpegts -vcodec copy -acodec copy -vbsf h264_mp4toannexb LOW.TS
  7. Segment the stream:
    segmenter.exe VIDEO-96KBPS.TS 10 STREAMNAME/LOW STREAMNAME/LOW-TMP.M3U8 http://mydomain.com/virtual/path/to/STREAMNAME/
  8. Optional: Make all URLs relative in STREAMNAME/LOW.M3U8… Apple’s mediastreamvalidator will issue warnings if you don’t do this. I use a sed script:
    sed.exe "s/http\:\/\/mydomain.com\/virtual\/path\/to\/STREAMNAME\///" STREAMNAME/LOW-TMP.M3U8 > LOW.M3U8
  9. Repeat steps 5-8 for Medium (256 kb/s) and High (800 kb/s) bitrates.
  10. Generate the final playlist:
    echo med.m3u8>> STREAMNAME\STREAMNAME.M3U8
    echo high.m3u8>> STREAMNAME\STREAMNAME.M3U8
    echo low.m3u8>> STREAMNAME\STREAMNAME.M3U8
    echo audio.m3u8>> STREAMNAME\STREAMNAME.M3U8

So what’s going on above?  What’s the gist of an HLS stream?  Briefly, setting up a static compliant HLS stream involves the following: Encode your video at varying bitrates. Then slice each of those encoded videos into even chunks (10 seconds recommended) and output a playlist. A master playlist (aka variant playlist) points to each bitrate-playlist. Let the player, based on bandwidth constraints, figure out what bitrate it wants on the fly. If it has to switch to a different bitrate-playlist, it merges segments by matching up the audio (which should be encoded identically between streams). This seems to fit Apple’s general philosophy: your video will stream; it will work regardless of your bandwidth limitations.

A couple of important notes:

  • One benefit: No streaming server is needed to make this work. Any HTTP server will work, including Apache and IIS.
  • As mentioned here“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.”(You have to wonder if any of this is to simply protect AT&T’s bandwidth?)
  • Apple has proposed HLS as a standard.
  • So far as I can tell, Apple doesn’t provide an encoder to produce the needed MPEG-2 Transport Stream, nor does any SINGLE Apple tool to encode and dump out the necessary files for HLS. They do give you command-linetools for segmenting, creating a variant playlist (aka master-playlist), and verifying streams. This seems like a glaring omission from QuickTime Pro honestly.  It does seem from forum posts that QuickTime Pro can output a format that the Apple’s  segmenter (a command line tool on MacOS) can deal with though.
  • It’s all standards based (x264, AAC/MP3, MPEG2-TS) with recommendations for settings provided in theirFAQ. So we should be able to cobble the necessary files together from free tools that run on Windows. Almost all of them are open source…

So off I went, in an attempt to figure all this out from available, free tools. I came across a very helpful article atwww.ioncannon.netiPhone HTTP Streaming with FFMpeg and an Open Source Segmenter and initially got a stream up and running quite quickly.  But the bitrates of the resultant streams just didn’t come close to the bitrates I wanted.  (Knowing how HLS works, it seems it would be fairly important to have bitrates that come close to what is advertised in the main playlist.)  I spent the next few days trying my best to getFFMPEG to cooperate, but to no avail… on a 150 kbit/s stream, it produced 220 kbit/s… on a 800 kbit/s stream, it produced a 250 kbit/s stream. On top of this, if I told FFMPEG to use AAC, the audio sounded horrible (MP3 was much better at the same bitrate!)

After days of experimentation, I finally got the results I wanted from free tools for creating HTTP Streams for use with iPhone, iPad, iPod. The information out there on how to get these tools to do what you want is pretty horrible. Especially for the video processing layman like myself, who “just wants it to work!”

In summary, FFMPEG fails on two fronts here:

  • FFMPEG is horrible at AAC audio encoding. The resultant audio sounds horrible. The free NeroAacEnc does a much better job.  Yes, we could use MP3, but AAC supposedly provides better sound at similar compression rates versus MP3.
  • FFMPEG simply doesn’t (by any of my tests) come close to honoring a recommended bitrate for x264 video encoding.  I have tried suggestions from  theFFmpeg x264 encoding guide, including two pass encoding, but to no avail.  Video bitrates were simply way off.

So I am using a combination of executables: ffmpeg (extract audio for Nero), Nero AAC Encoder (audio encoding), x264 (video encoding), ffmpeg (mux audio/video to create TS file), and an open source segmenter (to create m3u8 for each bitrate). The resulting output looks great, and at least comes pretty close to desired bitrate… finally!

Hope this helps someone out there!

HLS(HTTP Live Streaming)协议之m3u8文件生成方式

HLS(HTTP Live Streaming)是Apple的动态码率自适应技术。主要用于PC和Apple终端的音视频服务。包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件。 ...
  • sunxiaopengsun
  • sunxiaopengsun
  • 2017年04月08日 08:23
  • 733


  • acm2008
  • acm2008
  • 2014年12月17日 14:05
  • 4421

[HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)(3)

http://www.cuplayer.com/player/PlayerCodeAs/2014/03071219_3.html 6、 安装segmenter svn co htt...
  • mengzhengjie
  • mengzhengjie
  • 2015年10月08日 13:43
  • 3442


HLS,Http Live Streaming 是由Apple公司定义的用于实时流传输的协议,HLS基于HTTP协议实现,传输内容包括两部分,一是M3U8描述文件,二是TS媒体文件。 1、M3U8文件...
  • Guofengpu
  • Guofengpu
  • 2017年02月08日 09:20
  • 2775

基于Nginx,搭建HLS(HTTP Live Streaming)server,点播&直播

Ubuntu中Nginx的安装与配置点播(VOD)/usr/local/nginx/html 放入index_SIM1.m3u8,以及m3u8文件夹 访问ip/index_SIM1.m3u8 即可...
  • williamvon
  • williamvon
  • 2016年08月25日 10:24
  • 5093


在开发中可能会需要一些数据源,这在网上找到一批电视直播数据源。在这里做个记录,方便自己以后查找。 {☆央视频道☆} CCTV1-综合,
  • u013354805
  • u013354805
  • 2015年11月24日 16:49
  • 488925


参照 http://tools.ietf.org/html/draft-pantos-http-live-streaming-08, 可以对M3U8有比较系统的认识。           H...
  • Matthew_Fan
  • Matthew_Fan
  • 2012年07月28日 15:29
  • 39741

Android 离线播放 (HLS)m3u8文件

关于HLS(m3u8)文件的介绍 在baidu一下有很多很好的文章了,在这我就不多说了。 android是在sdk4.0以后才开始支持m3u8的播放,此文章便是在sdk4.0以后才适用。 sdk4...
  • zuoer8676
  • zuoer8676
  • 2015年08月27日 09:48
  • 2355


出处:iOS流媒体开发之三:HLS直播(M3U8)回看和下载功能的实现 概要 流媒体开发第一篇文章就说要把这些不是随便就可以百度到的知识献给“简书”,拖了一个多月了,总算弄完了...
  • Companion_1314
  • Companion_1314
  • 2016年09月27日 11:55
  • 3915


HTTP Live Streaming(HLS)是苹果公司(Apple Inc.)实现的基于HTTP的流媒体传输协议,可实现流媒体的直播和点播,主要应用在iOS系统,为iOS设备(如iPhone、iP...
  • wdxin1322
  • wdxin1322
  • 2016年12月28日 19:25
  • 6204
您举报文章:HLS(http live streaming) m3u8自制