宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > FFmpeg技术

[ffmpeg]Encoding HLS with ffmpeg编码

2016-10-13 00:16:48 点击量:30 标签: 收藏本文

[ffmpeg]Encoding HLS with ffmpeg

  1. For starters here is the command to create a stream that is GOP aligned, that is has keyframes at regular intervals that can be nicely segmented. It is recommended to pull down and compile the latest version of ffmpeg for this as some of the distros can be a bit out of date. 
  2.  
  3. ffmpeg -y -i %(filename)s -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r %(fps)s -profile:v baseline -b:v %(bitrate)sk -maxrate %(bitrate)sk  -force_key_frames %(keyframe_str)s -s %(width)sx%(height)s %(target_dir)s%(newFile)s 
  4.  
  5. The s%(filename) are in Python format where the %s(var) bit gets replaced with your value. 
  6.  
  7. For example: 
  8.  
  9. ffmpeg -y -i infile.mp4 -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r 25 -profile:v baseline -b:v 2000k -maxrate 2500k  -force_key_frames 50s -s 640×360 /tmp/newfile.ts 
  10.  
  11. I like to do segmenting as a second step just to keep the whole process a bit cleaner, especially if you want to produce multiple formats off the same initial encode: 
  12.  
  13. ffmpeg -y -i %(target_dir)s%(filename)s -codec copy -map 0 -f segment -segment_list %(target_dir)sindex_%(bitrate)s.m3u8 -segment_time %(segment_size)s  -segment_list_type m3u8 %(filenameNoExt)s_%(count)s.ts 
  14.  
  15. Or you could wrap it all into one command e.g. 
  16.  
  17. ffmpeg -y -i %(filename)s -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r %(fps)s -profile:v baseline -b:v %(bitrate)sk -maxrate %(bitrate)sk  -force_key_frames %(keyframe_str)s -s %(width)sx%(height)s %-f segment -segment_list %(target_dir)sindex_%(bitrate)s.m3u8 -segment_time %(segment_size)s  -segment_list_type m3u8 %(filenameNoExt)s_%(count)s.ts 
  18.  
  19. Working example: 
  20.  
  21. ffmpeg -y -i infile.mp4 -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r 25 -profile:v baseline -b:v 1500k -maxrate 2000k -force_key_frames 50 -s 640×360 -map 0 -flags -global_header -f segment -segment_list /tmp/index_1500.m3u8 -segment_time 10 -segment_format mpeg_ts -segment_list_type m3u8 /tmp/segment%05d.ts 
  22.  
  23. Here is an example of capturing from the desktop Linux and streaming straight to a local web server configured for low latency: 
  24.  
  25. ffmpeg -f x11grab -s `xdpyinfo | grep ‘dimensions:’|awk ‘{print $2}’` -r 25 -i :0.0 -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r 25 -profile:v baseline -b:v 1500k -x264opts keyint=25 -s 640×360 -map 0 -flags -global_header -f segment -segment_list index_1500.m3u8 -segment_time 1 -segment_format mpeg_ts -segment_list_type m3u8 -segment_list_flags +live -segment_list_size 2 segment%05d.ts 
  26.  
  27. Tested with: 
  28.  
  29. ffmpeg version N-51554-g1c0d8f2 Copyright (c) 2000-2013 the FFmpeg developers 
  30.  
  31.   built on Apr  7 2013 18:35:14 with llvm-gcc 4.2.1 (LLVM build 2336.11.00) 
  32.  
  33.   configuration: —enable-libx264 —enable-libfaac —enable-nonfree —enable-gpl 
  34.  
  35.   libavutil      52. 24.100 / 52. 24.100 
  36.  
  37.   libavcodec     55.  2.100 / 55.  2.100 
  38.  
  39.   libavformat    55.  1.100 / 55.  1.100 
  40.  
  41.   libavdevice    55.  0.100 / 55.  0.100 
  42.  
  43.   libavfilter     3. 49.100 /  3. 49.100 
  44.  
  45.   libswscale      2.  2.100 /  2.  2.100 
  46.  
  47.   libswresample   0. 17.102 /  0. 17.102 
  48.  
  49.   libpostproc    52.  2.100 / 52.  2.100 
  50.  
  51. Hyper fast Audio and Video encoder 

 

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室