HLS (HTTP Live streaming) is Apple's dynamic bitrate adaptive technology. Audio and video services primarily for PC and Apple terminals. Includes an index file for M3U (8), a TS media shard file, and a key encryption string file.
The key to HLS is to generate m3u8 index files and TS Media shards, and I'll cover the generation of m3u8 and TS media shards by following several steps:
The first step ---Get the TS file:
TS (Transport stream) is a transport stream, standards developed in the MPEG2 document protocol, when the TS format is mainly for digital television transmission and the development of a very early years, can find a very complete online MPEG2 document introduction. You can refer to the Mpege-2 documentation standard in the TS Flow introduction to learn the format.
Now we download high definition movies in MKV format mostly, early movies may be a rmvb and Avi majority, earlier even have MPG format, now popular video website download video is basically FLV format. These formats are non-TS format, but it doesn't matter, now the video transcoding software is also very much, we can use the following two ways to Transcode.
1, through the format of the factory software, this is a more mature software, online Baidu download can, but only software, not conducive to the direct development of late source code;
: http://www.pcfreetime.com/CN/index.html
2, through the ffmpeg format conversion, the project for the Open source project, we in the actual development process can be directly integrated with the source code, (the specific integration of this article does not explain, the later will be on how to encapsulate call FFmpeg to make a corresponding introduction). Currently we just want to get TS file for production m3u8 index file and TS shard only, directly download FFmpeg executable program, through Ffmpeg.exe conversion can:
: http://ffmpeg.org/
Enter the directory where Ffmpeg.exe is located by command-line mode and enter:ffmpeg.exe-i xxx.flv xxx.ts on the command line, such as:
Figure 1
Step Two --Generate m3u8 index file and TS Media shard
1, under the m3u8 source,
:
Https://github.com/johnf/m3u8-segmenter/archive/master.zip The source code for this address is mostly compiled on Linux, but can also be modified to compile under Windows.
Source code download for Windows:
Official website: http://www.espend.de/artikel/iphone-ipad-ipod-http-streaming-segmenter-and-m3u8-windows.html Source address/HTTP/ code.google.com/p/httpsegmenter/but also rely on the FFmpeg library, slightly modified can be.
In fact, the above two path of the source is the same drop, the following is the Germans modified to write, see the suffix de know, may need to FQ to open.
Here is the snippet fragment of the code that intercepts the SEGMENTER.C:
Do { DoubleSegment_time =0.0; Avpacket packet; DoublePacketstarttime =0.0; DoublePacketduration =0.0; if(!Decode_done) {Decode_done= Av_read_frame (IC, &packet); if(!Decode_done) { if(Packet.stream_index! = Video_index &&Packet.stream_index!=Audio_index) {Av_free_packet (&packet); Continue; } TimeStamp= (Double) (packet.pts) * (Double) (Ic->streams[packet.stream_index]->time_base.num)/ (Double) (ic->streams[packet.stream_index]->Time_base.den); if(Av_dup_packet (&packet) <0) {fprintf (stderr,"Could not duplicate packet\n"); Av_free_packet (&packet); Break; } insertpacket (Streamlace,&packet, TimeStamp); } } if(Countpackets (Streamlace) < -&&!Decode_done) { /*Allow the "queue to fill up" so "the packets can be sorted properly*/ Continue; } if(!removepacket (Streamlace, &packet)) { if(decode_done) {/*the queue is empty and we are doing*/ Break; } assert (Decode_done); Continue; } packetstarttime= (Double) (packet.pts) * (Double) (Ic->streams[packet.stream_index]->time_base.num)/ (Double) (ic->streams[packet.stream_index]->Time_base.den); Packetduration= (Double) (packet.duration) * (Double) (Ic->streams[packet.stream_index]->time_base.num)/ (Double) (ic->streams[packet.stream_index]->Time_base.den); #if!defined (ndebug) && (defined (DEBUG) | | defined (_DEBUG))if(Av_log_get_level () >=av_log_verbose) fprintf (stderr,"Stream%i, packet [%f,%f) \ n", Packet.stream_index, Packetstarttime, Packetstarttime+packetduration);#endifsegment_duration= Packetstarttime + Packetduration-Prev_segment_time; //Note:segments is supposed to start on a keyframe. //If The Keyframe interval and segment duration do not match//forcing the segment creation for "Better Seeking Behavior"//Would result in decoding artifacts after seeking or stream switching. if(Packet.stream_index = = Video_index && (packet.flags & Av_pkt_flag_key | |strict_segment_duration)) {Segment_time=Packetstarttime; } Else if(Video_index <0) {Segment_time=Packetstarttime; } Else{segment_time=Prev_segment_time; } if(Segment_time-prev_segment_time + segment_duration_error_tolerance >target_segment_duration+extra_duration_needed) {Avio_flush (OC-pb); Avio_close (OC-pb); //Keep track of accumulated rounding error to account for it in later chunks.Segment_duration = Segment_time-Prev_segment_time; Rounded_segment_duration= (int) (Segment_duration +0.5); extra_duration_needed+= (Double) Rounded_segment_duration-segment_duration; Updateplaylist (playlist, Playlist_filename, Output_filename, Output_index, rounded_segment_duration); _snprintf (Output_filename, strlen (output_prefix)+ the,"%s-%u.ts", Output_prefix, + +Output_index); if(Avio_open (&OC->PB, Output_filename, Avio_flag_write) <0) {fprintf (stderr,"Could not open '%s ' \ n", Output_filename); Break; } //Close when we find the ' kill ' file if(kill_file) {file* fp = fopen ("Kill","RB"); if(FP) {fprintf (stderr,"user Abort:found kill file\n"); Fclose (FP); Remove ("Kill"); Decode_done=1; Removeallpackets (Streamlace); }} prev_segment_time=Segment_time; } ret= Av_interleaved_write_frame (OC, &packet); if(Ret <0) {fprintf (stderr,"warning:could not write frame of stream\n"); } Else if(Ret >0) {fprintf (stderr,"End of Stream requested\n"); Av_free_packet (&packet); Break; } av_free_packet (&packet); } while(!decode_done | | countpackets (STREAMLACE) >0);
2, the download down the source code directly in VS compiled to generate EXE can, as I generated exe for M3u8.exe:
Figure 2
3. Enter the directory from the command line and enter: M3u8.exe-d 10-x m3u8list.m3u8 to generate the. m3u8 file and the TS Shard file, the m3u8list.m3u8 and -1.ts, -2.ts, and -3.ts files of the 2 catalog files.
Figure 3
4, such as the directory list in Figure 2, directly with VLC player can play the m3u8list.m3u8 file, use WordPad to view the contents of the m3u8 file:
#EXTM3U
#EXT-x-targetduration:10
#EXTINF: 10,
-1.ts
#EXTINF: 10,
-2.ts
#EXTINF: 9,
-3.ts
#EXT-x-endlist
All right, it's done! We can directly play m3u8list.m3u8 and -1.ts, -2.ts, -3.ts files, or directly with the HTTP protocol to transfer these files, it became the HLS protocol
The m3u8 file generation method for HLS (HTTP Live Streaming) protocol