Live broadcast for cows Part 4

I. Introduction

In Part 3 of Live Broadcasting for Cows,  we talked about how to build an RTMP live broadcast server. A few days ago, I was tossing around with Android live push and pull streams but had no results. I sorted out how Android loads SO dynamic libraries. I wanted to follow this path to JNI, NDK, JSBridge, and Python calls to SO are sorted out together. This is like a function call A->B->CD->E. However, if there is no return, it is easy for the stack to overflow. Let’s go back to the video-related things first. , I will talk about the above knowledge points later.

2. HLS protocol

Live Broadcasting for Milk Part 2   introduced the video application layer HTTP and RTMP protocols. Today we will take a look at the HLS protocol.

1. HLS concept

HLS, the full name of HTTP Live Stream, is a streaming media network protocol based on HTTP launched by Apple. Its working principle is to divide the entire stream into small files based on HTTP protocol for downloading.

2. HLS protocol provisions

  • The packaging format of the video is TS

  • The video encoding format is H.264, and the audio format is MP3, AAC.

  • Control the played file m3u8

Note: The first load of playback based on HLS is close to 10 seconds, while RTMP is about 3 seconds. However, HLS is not affected by firewalls, and in addition to the first load of HLS, subsequent small TS files will be downloaded through multiple threads in advance because of the m3u8 index file. .The efficiency is still good.

3. HLS video collection and playback process

Note: The above picture is from the Internet, borrow it.

  • The collection end can be video in any format, and the communication can also be in any protocol.

  • The video server performs H.264 encoding on the original video stream, and the video encoding and decoding can be viewed as  one of the live broadcasts for cows .

  • Then slice the video and generate the index file m3u8 and many small ts files after slicing.

3. Linux slicing environment construction

1. Install ffmpeg 

apt-get install ffmpeg

ffmpeg is a set of open source software that can be used to record, convert digital audio and video, and convert them into streams.

2. Install ffmpeg support library

apt-get install libavformat-dev

m3u8-segmenter needs to depend on the libavformat-dev library.

3. Compile and install m3u8-segmenter

Download address: https://github.com/m3u8-segmenter/m3u8-segmenter

Installation dependencies: Not sure if they are all needed, just install them together.

apt install curl build-essential automake automake1.11 libavformat-dev pkg-config ffmpeg
aclocal
automake -ac
autoconf
./configure
make && make install

4. Use ffmpeg to convert mp4 to TS

例子:ffmpeg -y -i channel.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb channel.ts

5. Use segmenter to slice the TS file and generate m3u8

例:m3u8-segmenter -i channel.ts -d 1 -p channel -m channel.m3u8 -u http://118.31.5.244/

Cut into pieces every 1 second, and then specify the path of the web

Note: After ffmpeg and m3u8-segmenter are installed, the parameters of the command can be viewed using man.

4. Introduction to m3u8 and ts files

m3u8 is actually an index file, which records the download address of TS files.

The content of channel.m3u8 generated by the above example is as follows

ts file is a video encapsulation format, the full name is MPEG-TS. Each small segment can be played independently. The file generated after slicing by the above m3u8-segmenter command is as follows.

Place the generated m3u8 and all TS files in the Nginx directory, and then integrate Video.js on the front end to play.

Below is the network request for Taobao students' recorded courses. You can see that small TS files are being downloaded. This can also protect copyright to a certain extent, unless you are willing to merge TS files together.

http://v.xue.taobao.com/learn.htm?spm=a2174.7365753.0.0.y556Ba&courseId=53566

If it is a live broadcast, m3u8 and TS files need to be generated dynamically, which is much more complicated. Please play it when you have time.

Note: When I was writing this article, I remembered that I had stayed in the project team of Taobao classmates for a while, but the video came from a colleague from Xunlei who was responsible for connecting with people from the Taobao video team. I seemed to be maintaining VSearch data at the time, and I was always interested in the video. I didn’t know about it, but I said that I worked with three people who made videos in different project teams, one from Kuaibo and one from Shanda. At that time, the girls who talked most with them were girls, and they didn’t talk much about technology. They seemed to be horny when it came to video technology. ha.

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/133455087