HTTP 直播:Linux 的噩梦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10971039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Live Streaming : The Linux nightmare
提问by
I'm working on a music VOD app on iPhone, and thanks to Apple guidelines, I have to run a HTTP Live Streaming in order to be accepted on the AppStore. But, since Apple doesn't care about 98% of servers on earth, they don't provide their so magical HTTP Live Streaming Tools for Linux-based systems. And from this point, the nightmare starts.
我正在 iPhone 上开发音乐 VOD 应用程序,由于 Apple 的指导方针,我必须运行 HTTP Live Streaming 才能在 AppStore 上被接受。但是,由于 Apple 并不关心地球上 98% 的服务器,因此他们不会为基于 Linux 的系统提供如此神奇的 HTTP 实时流媒体工具。而从这一刻开始,噩梦就开始了。
My goal is simple : Take an MP3, segmentate it and generate a simple .m3u8 index file. I googled "HTTP Live Streaming Linux" and "Oh great ! lots of people have already done that"!
我的目标很简单:取一个 MP3,对其进行分段并生成一个简单的 .m3u8 索引文件。我在谷歌上搜索了“HTTP Live Streaming Linux”和“哦,太棒了!很多人已经这样做了”!
First, I visited the (so famous) post by Carson McDonald. Result : the svn segmentate.c was old, buggy and a nightmare to compile (Nobody in this world can precise what version of ffmpeg they are using !). Then I came across the Carson's git repo, but too bad, there is a lot of annoying ruby stuff and live_segmenter.c can't take mp3 files.
首先,我访问了 Carson McDonald 的(非常有名的)帖子。结果:svn segmentate.c 很旧,有问题,而且是编译的噩梦(这个世界上没有人可以准确地知道他们使用的是哪个版本的 ffmpeg!)。然后我遇到了 Carson 的 git repo,但太糟糕了,有很多烦人的 ruby 东西,而且 live_segmenter.c 不能接受 mp3 文件。
Then I searched more deeply. I found this stackoverflow topic, and it's exactly what I want to do. So I have followed the advice from juuni to use this script (httpsegmenter). Result: Impossible to compile anything, 2 days of works and finally I managed to compile it (ffmpeg 8.1 w/ httpsegmenter rev17). And no, this is not a good script, it does take mp3 files, but the ts files generated and the index file can't be read by a player.
然后我更深入地寻找。我找到了这个 stackoverflow 主题,这正是我想要做的。所以我遵循了 juuni 的建议来使用这个脚本 (httpsegmenter)。结果:无法编译任何东西,2 天的工作,最后我设法编译它(ffmpeg 8.1 w/ httpsegmenter rev17)。不,这不是一个好的脚本,它确实需要 mp3 文件,但是生成的 ts 文件和索引文件无法被播放器读取。
Then the author of the post krisbulman, came with a solution, and even gave a patched version of m3u8-segmenter by his own (git repo). I test it : doesn't compile, do nothing. So I took the original version from johnf https://github.com/johnf/m3u8-segmenter. I managed to compile and miracle it works (not really). I used this command line (ffmpeg 0.8.1):
然后krisbulman这个帖子的作者,来了一个解决方案,甚至还自己给了一个m3u8-segmenter的补丁版本(git repo)。我测试它:不编译,什么都不做。所以我从 johnf https://github.com/johnf/m3u8-segmenter 获取了原始版本。我设法编译并奇迹般地工作(不是真的)。我使用了这个命令行(ffmpeg 0.8.1):
ffmpeg -er 4 -i music.mp3 -f mpegts -acodec libmp3lame -ar 44100 -ab 128k -vn - | m3u8-segmenter -i - -d 10 -p outputdir/prefix -m outputdir/output.m3u8 -u http://test.com/
This script encode my mp3 file (it takes 4 seconds, too long), and pass it to the m3u8-segmenter to segment it into 10 seconds .TS files.
此脚本对我的 mp3 文件进行编码(需要 4 秒,太长了),并将其传递给 m3u8-segmenter 以将其分段为 10 秒的 .TS 文件。
I tested this stream with Apple's mediastreamvalidator on my mac, and it said that it was OK. So i played it into quicktime, but there is about 0.2 seconds blank between each .TS files !!
我在我的 Mac 上用 Apple 的 mediastreamvalidator 测试了这个流,它说没问题。所以我把它播放到 quicktime,但每个 .TS 文件之间有大约 0.2 秒的空白!!
So here is my situation, it's a nightmare, I can't get a simple mp3 stream over the HLS protocol. Is there a simple WORKING solution to segmentate a mp3 ? Why can't I directly segmentate the mp3 file into multiple mp3 files like Apple's mediafilesegmenter does?
所以这是我的情况,这是一场噩梦,我无法通过 HLS 协议获得简单的 mp3 流。是否有一个简单的 WORKING 解决方案来分割 mp3 ?为什么我不能像 Apple 的 mediafilesegmenter 那样直接将 mp3 文件分割成多个 mp3 文件?
回答by paulsm4
Your English is fine.
你的英文很好。
Your frustration is apparent.
你的沮丧是显而易见的。
Q: What's the real issue here? It sounds like you just need a working HLS server, correct? Because of Apple requirements, correct?
问:这里真正的问题是什么?听起来您只需要一个有效的 HLS 服务器,对吗?因为苹果的要求,对吗?
Can you use any of the ready-made implementations listed here:
你能使用这里列出的任何现成的实现吗:
回答by vchola
Use libfaac insteam of libmp3lame which eliminates the 0.2 second break.
使用 libmp3lame 的 libfaac insteam 可以消除 0.2 秒的中断。
回答by jeremy
I know this is an old question, but I am using this in VLC:
我知道这是一个老问题,但我在 VLC 中使用它:
## To start playing the playlist out to the encoder
cvlc -vvv playlist.m3u --sout rtp:127.0.0.1 --ttl 2
## To start the encoder
cvlc rtp:// --sout='#transcode{acodec=mp3,ab=96}:duplicate{dst=std{access=livehttp{seglen=10,splitanywhere=true,delsegs=true,numsegs=15,index=/var/www/vlctest/mystream.m3u8,index-url=http://IPANDPORT/vlctest/mystream-########.ts},mux=ts,dst=/var/www/vlctest/mystream-########.ts},select=audio}'
I had problems if I didn't stream the playlist file to another copy of VLC, the first step is optional if you already have a live streaming source. (but you can use any source for the "encoder" portion).
如果我没有将播放列表文件流式传输到 VLC 的另一个副本,我会遇到问题,如果您已经有实时流式传输源,则第一步是可选的。(但您可以将任何来源用于“编码器”部分)。
回答by Mingfei Yan
You could try to use our media services on Windows Azure platform: http://mingfeiy.com/how-to-generate-http-live-streaming-hls-content-using-windows-azure-media-services/
您可以尝试在 Windows Azure 平台上使用我们的媒体服务:http: //mingfeiy.com/how-to-generate-http-live-streaming-hls-content-using-windows-azure-media-services/
You could encode and stream your video in HLS format by using our portal with no configuration and coding required.
您可以使用我们的门户以 HLS 格式对视频进行编码和流式传输,无需配置和编码。
回答by kubanoid
For live streaming only, you should try Nginx with RTMP module for this one. https://github.com/arut/nginx-rtmp-moduleLive HLS works pretty good but with looooong buffer. However, it does not support on-demand HLS streaming.
仅对于实时流媒体,您应该为此尝试使用带有 RTMP 模块的 Nginx。https://github.com/arut/nginx-rtmp-moduleLive HLS 运行良好,但带有 looooong 缓冲区。但是,它不支持按需 HLS 流。
Piece of module`s config for example
例如一块模块的配置
# HLS requires libavformat & should be configured as a separate
# NGINX module in addition to nginx-rtmp-module:
# ./configure ... --add-module=/path/to/nginx-rtmp-module/hls ...
# For HLS to work please create a directory in tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use 'exec' feature.
#
application hls {
live on;
hls on;
hls_path /tmp/app;
hls_fragment 5s;
}
回答by Francis Shanahan
Elastic Transcoder Service - if you don't need AES encryption just throw your MP3 in an S3 bucket and be done with it:
Elastic Transcoder Service - 如果您不需要 AES 加密,只需将您的 MP3 放入 S3 存储桶中即可完成:
http://aws.amazon.com/elastictranscoder/
http://aws.amazon.com/elastictranscoder/
You can then even add Cloudfront CDN support. (P.S. I fully appreciate your pain, this whole space is a nightmare).
然后,您甚至可以添加 Cloudfront CDN 支持。(PS 我完全理解你的痛苦,整个空间都是一场噩梦)。
回答by salfter
What problems were you having with httpsegmenter? It's a single C source file that only links against some libraries provided by ffmpeg (or libav). I maintain a Gentoo ebuild for it, as I use it to time-shift talk radio. If you're running Gentoo, building is as simple as this:
你在使用 httpsegmenter 时遇到了什么问题?它是一个单独的 C 源文件,仅链接到 ffmpeg(或 libav)提供的一些库。我为它维护了一个 Gentoo ebuild,因为我用它来时移谈话广播。如果您正在运行 Gentoo,构建就像这样简单:
sudo bash -l
layman -S
layman -a salfter
echo media-video/httpsegmenter ~\* >>/etc/portage/package.accept_keywords
emerge httpsegmenter
exit
On Ubuntu, I had to make sure libavutil-dev and libavformat-dev were both installed, so the build looks something like this:
在 Ubuntu 上,我必须确保 libavutil-dev 和 libavformat-dev 都已安装,因此构建看起来像这样:
sudo apt-get install libavutil-dev libavformat-dev
git clone https://gitlab.com/salfter/httpsegmenter.git
cd httpsegmenter
make -f Makefile.txt
sudo make -f Makefile.txt install
Once it's built (and once I have an audio source URL), usage is fairly simple: curl to stream the audio, ffmpeg to transcode it from whatever it is at the source (often MP3) to AAC, and segmenter to chunk it up:
一旦它被构建起来(并且一旦我有了一个音频源 URL),使用就相当简单:curl 来流式传输音频,ffmpeg 将它从源(通常是 MP3)的任何内容转码为 AAC,以及分段器将其分块:
curl -m 3600 http://invalid.tld/stream | \
ffmpeg -i - -acodec libvo_aacenc -ac 1 -ab 32k -f mpegts - 2>/dev/null | \
segmenter -i - -d 20 -o ExampleStream -x ExampleStream.m3u8 2>/dev/null
This grabs one hour of streaming audio (needs to be MP3 or AAC, not Flash), transcodes it to 32 kbps mono AAC, and chunks it up for HTTP live streaming. Have it dump into a directory served up by your webserver and you're good to go.
这会抓取一小时的流音频(需要是 MP3 或 AAC,而不是 Flash),将其转码为 32 kbps 单声道 AAC,并将其分块用于 HTTP 实时流媒体。将其转储到您的网络服务器提供的目录中,您就可以开始使用了。
Once the show's done, converting to a single .m4a that can be served up as a podcast is also simple:
节目完成后,转换为可以作为播客提供的单个 .m4a 也很简单:
cat `ls -rt ExampleStream-*.ts` | \
ffmpeg -i - -acodec copy -absf aac_adtstoasc ExampleStream.m4a 2>/dev/null