Windows uses ffmpeg to convert MP4 to m3u8 using detailed parameters, adding watermark and compression to video

Table of contents

background:

1. What is m3u8:

2. Why use m3u8:

3. Install ffmpeg:

1. Unzip directly after downloading:

2. Configure environment variables:

4. Start converting m3u8:

5. Add watermark and compression to video:

1. Add watermark image to video

2. The size of the output video

3. The starting file of the output file

4. The minimum and maximum size of the output file (will affect the video quality)


background:

        Recently, the company purchased overseas courses to promote to colleges and universities; therefore, it is necessary to develop an on-demand platform, but in the face of complex customer groups, it is necessary to solve the problems of user physical examination and anti-leeching; therefore, we chose m3u8.

1. What is m3u8:

        The M3U8 video format is also a kind of M3U, but its encoding format is UTF-8 format. M3U is encoded with the Latin-1 character set. The M3U8 format is characterized by a directory information or file.

2. Why use m3u8:

        With the increasingly strong demand of customers to play on the mobile terminal, our transcoding software began to output mp4 format files. After testing, we found that the compression rate of excellent software compressed mp4 and flv is almost the same, so we unified the output format as mp4. The mp4 files processed by our software can realize drag-and-drop playback without buffering (fast-forward playback), and also solve the problem of some video files with sound but no picture, which has won praise from customers.

        With the increase of customers, the disadvantages of mp4 file playback are becoming more and more prominent, mainly in two aspects. First, when the video duration is relatively long, the key frame elements of mp4 are often very large, and it takes a long time to load to start playing. If the network speed is not good, it will take more than 20 seconds to buffer and load, and the customer is already impatient. Second, when the user opens a video to play, the browser will continue to request to download the mp4 file until the download is complete. Even if the user pauses the video playback, the browser will continue to download. If the video file is 500M, it will request the server to download 500M file, if it is 1G, it will download 1G continuously, causing a lot of waste and pressure on the server hard disk and broadband.

        So we refer to the playback files of large video website systems such as Youku Tudou, and find that their video files are played in segments, that is, a large video file is divided into N segments according to a certain size or duration. Such an advantage The loading speed of opening the video is fast, and it can be played in seconds. Another advantage is that when the video plays the Nth segment, the browser will download the N+1 segment, and the N+2 segment will not be downloaded, which greatly relieves the pressure on the server hard disk and broadband , but know that the high cost of broadband is the biggest pressure on video sites. This video file processing method has been well received by a large number of customers.

        But soon we discovered a very serious problem. The video of the xml segment list is also helpless on IOS. In order to play the video on the IOS device, the mp4 file must be reserved for the IOS device to play. So the m3u8 playlist came on the scene.

        m3u8 is a new playback format developed by Apple. This playback format supports the mainstream browsers of windows, androis, and ios devices currently on the market. The same video file can be played in the flash environment, and can also be played in the html5 environment without flash. Play, its advantages are not limited to this, it can realize the automatic switching of various bit rates under different network speeds, automatically switch high-definition videos when the network speed is good, and automatically play low-definition files when the network speed is slow, and can also realize streaming Encryption (the video file itself is encrypted), segmented download and playback, drag and drop playback at any point in time, random video file advertisement insertion, etc., so the latest version of the cloud transcoding video system only uses m3u8 as the only playback format and gives up other format output.

3. Install ffmpeg:

First download the ffmpeg file available for windows (I will talk about other chapters in the Linux environment)

The URL is: https://github.com/BtbN/FFmpeg-Builds/releases

1. Unzip directly after downloading:

After the download is complete, decompress the compressed package. After the completion, the ffmpeg.exe file in the bin directory will be used when the program starts later.

2. Configure environment variables:

Add the path of bin in the ffmpeg folder to the path of the Windows system variable

After the configuration is complete, check whether the configuration is successful

Enter ffmpeg -version in CMD, if the following information appears, the configuration is successful

4. Start converting m3u8:

Prepare Mp4 file

open cmd

Execute the following command in CMD according to the file location

ffmpeg -i E:\demo\demo.MP4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls E:\demo\demo.m3u8

If the following prompt appears, the conversion is successful

Let's take a look at converting the files in the m3u8 folder

The file includes an m3u8 file and multiple ts files

We open the m3u8 file

It is found that the path of other video files is actually specified in the file

When we need to access the current video, we only need to point the address directly to the address of the current m3u8 file


5. Add watermark and compression to video:

ffmpeg -i E:\videos\video.mp4 -i E:\videos\logo.png -filter_complex overlay -profile:v baseline -level 3.0 -s 1024x1080 -start_number 0 -hls_time 0.5 -b:v 125k -bufsize 150k -hls_list_size 0 -f hls E:\videos\video\video.m3u8

1. Add watermark image to video

-i E:\videos\logo.png Add a watermark image to the video -filter_complex overlay position is located in the upper left corner of the video

Top left: -filter_complex overlay
Bottom left: -filter_complex overlay=0:Hh

Top right corner: -filter_complex overlay=Ww

Bottom right: -filter_complex overlay=Ww:Hh

2. The size of the output video

-s 1024x1080

3. The starting file of the output file

-start_number 0

4. The minimum and maximum size of the output file (will affect the video quality)

-b:v 125k -bufsize 150k

Reference link: Summary of common commands of FFmpeg_ffmpeg -i input.ts

Reference link: https://www.jianshu.com/p/e3f6c3705033
Reference link: https://www.jianshu.com/p/36475d6f4558
 


If this article is helpful to you, I am very happy to help you.

Of course, if you feel that there is something in the article that makes you feel unreasonable, or there is an easier way to implement it, or there is something that you can’t understand, I hope you can point it out in the comments after reading it, and I will read it as soon as possible reply you.

Guess you like

Origin blog.csdn.net/chenthe1/article/details/131008271