我们如何使用 ffmpeg 将实时 rtmp 流转码为实时 hls 流? [英] How can we transcode live rtmp stream to live hls stream using ffmpeg?
问题描述
我正在尝试将实时 rtmp 流实时转换为 hls 流.
I am trying to convert a live rtmp stream to hls stream on real time.
阅读后我有了一些想法
我能够将实时 rtmp 流转换为 hls,但不能在运行时转换.当我运行命令并测试任何 hsl 文件(.m3u8 和 .ts)时,我看不到,但是当我中断命令并检查那里时,我会根据需要获得 hls 文件.
i am able to convert the live rtmp stream to hls but not at run time. when i run the command and test for any hsl files (.m3u8 and .ts) i am not able to see but when i interrupt the command and check there i get the hls files as required.
我在谷歌上搜索了解决方案,但没有得到正确的答案.
I searched on google for solution but not able to get proper answer.
任何机构可以帮助我吗?
Can any body help me?
提前致谢...
推荐答案
这是使用任何输入文件或流进行 HLS 流传输的简短指南:
This is a short guide for HLS streaming with any input file or stream:
我遵循 user1390208 的方法,所以我只使用 FFMPEG 来生成 rtmp 流,然后我的服务器接收该流以提供 HLS.我没有使用 Unreal/Wowza/Adobe,而是使用带有 rtmp 模块的免费服务器 nginx,这很容易设置.简而言之,我就是这样做的:Any input file or stream ->ffmpeg ->rtmp ->nginx 服务器 ->HLS ->客户端
或更详细:
I am following user1390208's approach, so I use FFMPEG only to produce the rtmp stream which my server then receives to provide HLS. Instead of Unreal/Wowza/Adobe, I use the free server nginx with the rtmp module, which is quite easy to setup. This is how I do it in short: Any input file or stream -> ffmpeg -> rtmp -> nginx server -> HLS -> Client
or more detailed:
输入视频文件或流(http、rtmp 等) --> ffmpeg 实时转码为 x.264 + aac,输出到 rtmp --> nginx 获取 rtmp 并向用户(客户端)提供 HLS.因此,在客户端,您可以使用 VLC 或其他任何方式连接到 nginx 提供的 .m3u8 文件.
input video file or stream (http, rtmp, whatever) --> ffmpeg transcodes live to x.264 + aac, outputs to rtmp --> nginx takes the rtmp and serves a HLS to the user (client). So on the client side you can use VLC or whatever and connect to the .m3u8 file which is provided by nginx.
- 我遵循了 this nginx 设置指南.
- 这是我的 nginx 配置文件.
这就是我使用 ffmpeg 将我的输入文件转码为 rtmp 的方式:
- I followed this setup guide for nginx.
- This is my nginx config file.
This is how I use ffmpeg to transcode my input file to rtmp:
ffmpeg -re -i mydirectory/myfile.mkv -c:v libx264 -b:v 5M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv rtmp://localhost:12345/hls/mystream;
(.mkv 是 1080p 和 5.1 声音,根据您的输入,您应该使用较低的比特率!)
(the .mkv is 1080p with 5.1 sound, depending on your input, you should use lower bitrates!)
你从哪里得到 rtmp 流?
Where do you get the rtmp stream from?
- 文件?然后你可以完全使用我的方法.
任何带有流 Y 的服务器 X?那你得把ffmpeg命令改成:
- A file? Then you can use exactly my approach.
Any server X with a stream Y? Then you have to change the ffmpeg command to:
ffmpeg -re -i rtmp://theServerX/yourStreamY -c:v libx264 -b:v 5M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv rtmp://localhost:12345/hls/mystream;
或者如果您的 rtmp 流已经是 h.264/aac 编码的,您可以尝试使用 ffmpeg 中的 copy
选项将内容直接流式传输到 nginx.
or if your rtmp stream is already h.264/aac encoded, you could try to use the copy
option in ffmpeg to stream the content directly to nginx.
正如你在我的 nginx 配置文件中看到的:
As you see in my nginx config file:
- 我的 rtmp 服务器有一个名为hls"的应用程序".这就是描述 nginx 在哪里监听 ffmpeg 的 rtmp 流的部分,这就是为什么 ffmpeg 流到
rtmp://localhost:12345/hls/mystream;
- 我的 http 服务器有
location/hls
.这意味着在 VLC 中我可以连接到http://myServer:80/hls/mystream.m3u8
来访问 HLS 流.
- My rtmp server has an "application" called "hls". That's the part that describes where nginx listens to ffmpeg's rtmp stream and that's why ffmpeg streams to
rtmp://localhost:12345/hls/mystream;
- My http server has the
location /hls
. This means in VLC I can connect tohttp://myServer:80/hls/mystream.m3u8
to access the HLS stream.
一切都清楚了吗?直播快乐!
Is everything clear? Happy streaming!
这篇关于我们如何使用 ffmpeg 将实时 rtmp 流转码为实时 hls 流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!