Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The environment

  • Intel(R) Core(TM) I5-5200 U CPU @2.20GHz
  • Ubuntu 16.04 64 – bit
  • Ffmpeg static 3.2.2
  • dstat

preface

The ffMPEG transcoding parameters mentioned in this article are suitable for HLS(H264+AAC) scenarios, not for other scenarios.

Live environment

My video source here is UDP, and the transcoding commands obtained after a lot of tests are as follows

Ffmpeg -i "udp: / / @ 225.0.0.10:9001? buffer_size=1000000&fifo_size=1000000" -loglevel quiet -vcodec libx264 -acodec aac -strict -2 -preset ultrafast -tune zerolatency -b 4M -minrate 4M -maxrate 4M -bufsize 8M -ab 128k -r 25 -s 1280x720 -crf 28 -deinterlace -f flv RTMP: / / 10.0.0.188:2935 / live/live/indexCopy the code

In a live broadcast environment, -Preset is recommended to be set to ultrafast, which is the fastest transcoding to meet the demand of the terminal playback. In addition to ultrafast, the following options are available

  • ultrafast
  • superfast
  • veryfast
  • faster
  • fast
  • Medium – The default
  • slow
  • slower
  • veryslow

-b, -minrate, -maxrate, -ab are used to control the output bit rate. -bufsize is usually set to twice of -b. -r Sets the frame rate. -s controls output resolution; -deinterlace Remove video serrated, omit if the video source is ok; – Tune is set to zerolatency, which has some other options as follows

  • Film – use for high quality movie content; lowers deblocking
  • Animation – good for adults; uses higher deblocking and more reference frames
  • ◆ Grain — Each of the techniquesis molded from old, Grainy film Material
  • Stillimage – Good for slideshow-like content
  • Fastdecode — allows faster decoding by disabling certain filters
  • Zerolatency — Good for fast encoding and low-latency streaming
  • PSNR — Ignore this as it is only used for Codec development
  • Ssim – Ignore this as it is only used for Codec development

– CRF value range: 0 51,0 indicates loss-less, 18 people’s naked eyes are loss-less, the default value is 23,51 has the worst effect, the general principle is to increase the CRF value as much as possible under the premise of acceptable picture. For streaming media, 23, 28; -VF Scale =1280×720, setDAR =16:9 if the source is some 4:3 SD programs. If you need to generate live data at multiple bit rates, you can transcode slices in this way

ffmpeg -re -i rtmp://server/live/stream -acodec copy -vcodec libx264 -s 640x360 -b 500k -preset medium -profile:v baseline rtmp://server/live/baseline_500k -acodec copy -vcodec libx264 -s 480x272 -b 300k -preset medium -profile:v baseline rtmp://server/live/baseline_300k -acodec copy -vcodec libx264 -s 320x200 -b 150k -preset medium -profile:v baseline rtmp://server/live/baseline_150k -acodec aac -strict -2 -vn -ab 48k rtmp://server/live/audio_only_AAC_48k
Copy the code

On demand environment

Vod is generally ready-made films, transcoding slices in accordance with certain requirements

ffmpeg -re -i test.ts -vcodec libx264 -acodec aac -strict -2 -tune zerolatency -preset ultrafast -r 25 -g 50 -profile:v Baseline 3.0-CRF 28-VF scale= 1280x720-B: V 4M-minrate 4M-maxrate 4M-bufsize 8M-B: A 128K-F HLS -hls_list_size 0 index.m3u8Copy the code

There are some parameters that are the same as livestream, so I won’t go into that. -re indicates that the file is read according to the local frame rate. The total transcoding time is usually the length of the file. If -vcodec and -acodec are both copy, it can be deleted. -hls_list_size Specifies the number of TS files in the index file index.m3u8. If the default value is 0, only 5 ts files are reserved. -profile:v Sets the video compatibility. The value can be

  • Baseline 3.0 All devices -profile:v baseline -level 3.0
  • Baseline 3.1 iPhone 3G and later, iPod Touch 2nd Generation and later – Profile :v Baseline -level 3.1
  • Main 3.1 iPad (All Versions), Apple TV 2 and later, iPhone 4 and later – Profile: V main-level 3.1
  • Main 4.0 Apple TV 3 and later, iPad 2 and later, iPhone 4S and later – Profile: V main-level 4.0
  • High 4.0 Apple TV 3 and later, iPad 2 and later, iPhone 4S and later – Profile: V High-level 4.0
  • High 4.1 iPad 2 and later, iPhone 4S and later, iPhone 5C and later – Profile: V High-level 4.1
  • High 4.2 iPad Air and Later, iPhone 5S and Later – Profile: V High level 4.2

How to carry out multi-channel transcoding at the same time with limited hardware conditions is a very practical problem. I have carried out some tests on the i5 machine and used dstat tool to check the system resource usage at the same time

The figure above shows the use of system resources using -VF W3FDIF to remove video aliasing. The effect of -deinterlace will be better if it is also used to remove aliasing, as shown below

Without the need to serrate the video source, CPU usage drops by about 10%, as shown below

The resources

Trac.ffmpeg.org/wiki/Encode… Trac.ffmpeg.org/wiki/Stream… SLHCK. Info/video / 2017 /… Trac.ffmpeg.org/wiki/Encodi… Trac.ffmpeg.org/wiki/How%20…