Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Tutorial: How to "live stream" a media file

Tutorial: How to "live stream" a media file

作者头像
雪影
发布于 2019-05-28 02:56:49
发布于 2019-05-28 02:56:49
62500
代码可运行
举报
文章被收录于专栏:流媒体人生流媒体人生
运行总次数:0
代码可运行

How to "live stream" a media file

by Thorsten Pferdekämper

I have tried a while to setup a free (open source etc.) live streaming solution which is able to stream “anything” to a flash frontend. The basic idea is to stream TV from v4l2 (and similar), but I also wanted to stream files (movies). I found that most tutorials only show how to setup the streaming or only show how to get a flash player up and running. The whole roundtrip is not really described and has its own difficulties. This tutorial describes the whole “roundtrip” from a media file on your disk to displaying it in a browser. I know that there are easier ways to send a media file to some player in a browser and I also know that playing a file is not really live streaming. (This is why I have put it in double quotes.) However, it shows the principle and it might be easier to set this up as a first step.

Maybe important to note that this is targeted to a Linux server, I don’t know how and if it works with other operating systems.

Overview

The whole roundtrip looks as follows:

file → ffmpeg → crtmpserver → jwplayer/haxe ( → apache)

ffpmeg is used to convert the input to h264/aac in an MPEG-TS container and sends it to crtmpserver using UDP

crtmpserver acts as the RTMP server (you might have guessed this)

jwplayer can then play the stream. With using haXe, it is possible to create your own players

apache is the webserver

ffmpeg

The following command line converts <filename-of-movie> to h264/aac, puts it into an TS container and sends it using UDP. (I have split this on several lines for readability.)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
> ffmpeg -i "<filename-of-movie>" -re 
         -vcodec libx264 -vpre default -vpre baseline 
         -b 500000 -s 320x180 -strict experimental -g 25 -me_method zero 
         -acodec aac -ab 96000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb 
         -f mpegts udp://127.0.0.1:10000?pkt_size=1316

This command line simulates a live stream. It does not wait for anything to receive the stream, it just pumps the data through port 10000 at the speed the movie should play. (You should see some ffmpeg output about frames, fps etc.) This is achieved by the ”-re” which makes ffmpeg to read the data with (maximum) the framerate which the movie has. In addition, the UDP protokol does not wait for the answer of any receiver like TCP would do. (As far as I understand.)

In my tests, <filename-of-movie> always is in an AVI container, with DivX/XviD and MP3. It should, however, work as well with any other movie formats ffmpeg understands properly.

The ffmpeg command line above was taken from the wowza documentation. I have only experimented a bit to enhance the performance. So I have changed the ”-g 60” to ”-g 25” as it seems more appropriate to me and added the ”-me_method zero” as it was considered in the ffmpeg documentation as being faster. The ffmpeg documentation also mentions using ”-intra”, but this did not work. The stream never played in the flash player. (I don't have an explanation for that.)

ctmpserver

I have checked out and compiled the sources from svn as described on the crtmpserver website. After that, the server can be run. I have tried the same with other free rtmp servers and it did not work. E.g. I did never succeed with compiling red5. With crtmpserver, it just worked. It is also not that complicated to understand the coding. (Even if there could be some more comments…)

I.e. just get and compile crtmpserver as described in the crtmpserver documentation.

To enable it to receive an UPD TS stream, the following needs to be added to the file crtmpserver/builders/cmake/crtmpserver/crtmpserver.lua

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    {
            ip="0.0.0.0",
            port=10000,
            protocol="inboundUdpTs"
    }, 

This should be added to the acceptors part of the flvplayback application. This tells crtmpserver that there should be a stream in a TS container sent via UDP on port 10000. (See alsortmpserver.lua for the whole file.)

After changing this and running the ffmpeg command line above, run the server with the config file as argument, like

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
> cd crtmpserver/builders/cmake
> ./crtmpserver/crtmpserver crtmpserver/crtmpserver.lua

(I don't know yet why it only works from the cmake directory. Running from the crtmpserver directory itself did not work for me.)

UPDATE: This happens because cmake is used for building the development version of the server. It has all the binaries/libraries paths hardcoded in by the GCC linker. This is why you can't move around any binaries/libraries. If the production version is required (independent paths, maximum optimization, etc), make or packing scripts should be used instead. The documentation about installing/compiling the server will be changed to reflect that.

I keep having the issue that crtmpserver does not start up when I try it the first time after booting the machine. This is because there is another rtmp server (red5) installed as well. This needs to be stopped first, at least as long as it runs on the same port. I.e. all other rtmp servers should be stopped. On my sytsem, this is done like this:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
> /etc/rc2.d/S20red5-server stop

Apparently, this needs to be done as root.

Apart from some (maybe a lot) warnings, the crtmpserver output should contain something like

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
...basetsappprotocolhandler.cpp:49 Stream available (1): ts_2_257_256

Remember the “ts_2_257_256”, or whatever it exactly looks like. This is the name of the stream which is later needed in the flash player. The name might depend on the ffmpeg version and on the crtmpserver config and version. Generally, it has the format “ts_<id>_<audio-pid>_<video-pid>”.

crtmpserver now sends the stream as crtmpserver://<your-ip>/flvplayback/ts_2_257_256. This URI can be used to point a flash player to the stream. I have successfully done this with a very simple player written in Haxe and with JWPlayer as well.

But first, we need a webserver. Playing the streams from an embedded player using local files seems to have some security issues, so flash does not allow it easily.

apache

There's not much to say here. Install apache and find where the content needs to go. On my system, this is /var/www, which I believe is the default.

JWPlayer

The JWPlayer is a full-blown Flash video player. You can get it from here: http://www.longtailvideo.com/players/jw-flv-player/

Copy the file player.swf in a directory which can be reached by the web server (e.g. a subdirectory of /var/www) and embed it into an html file as described on the longtailvideo website. Alternatively, just use my test_jwplayer.html and put it in the same directory as player.swf. When using it, you need to change this part:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  streamer:'rtmp://zeus/flvplayback',file:'ts_2_257_256'

“zeus” is the name of my server, you need to change it to the name of your server. If unsure or it does not work, just use 127.0.0.1. Apparently, it will only work locally then…

You might also need to change the stream name (ts_2_257_256) to the one from crtmpserver's output.

Now make sure that ffmpeg and crtmpserver are running. Open a browser and type an url like

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    http://zeus/thorsten/jwplayer/test_jwplayer.html

Apparently, you need to change “zeus” to your machine's name or try 127.0.0.1. The rest is the path to the test_jwplayer.html file, which will also be different on your box.

After a few seconds of buffering, you should be able to watch the movie. It might not have the correct aspect ratio and it also might be a bit choppy, but it works in principle. (If it does not, restart ffmpeg and crtmpserver and reload the page.)

haXe

It seems that it can do even more, but for me, haXe (http://haxe.org/) is a free (open source) alternative to “develop flash”. I.e. you can write actionscript-like programs and compile them to .swf format. (I sometimes need to do things myself to really understand them.) I like this especially because I could write my own little flash stream player which I could experiment with. In theory, this could also be done with JWPlayer as this is open source as well. However, JWPlayer source is a bit complicated and I am not sure if you can compile it without the commercial flash development kit.

If you want to try the haXe player, install haxe (at least on debian, there is a package). Then create a file like Test.hx. Open it in an editor and change the appearances of “zeus” to your own server's name and change “ts_2_257_256” to the correct stream name. (I know that hardcoding these names is not a programming style which deserves to be called “style”, but again, it shows the principle.)

The file can then be compiled using the following command line from the directory where Test.hx sits in.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
> haxe -swf9 test.swf -main Test

This results in the file test.swf. This can then be embedded as any other flash player into a website, see e.g. test_haxe.html.

Put test.swf and test_haxe.html in a directory accessible by the web server. Open a browser and point it to the test_haxe.html page. Again, the movie should come up after a few seconds. (…if crtmpserver and ffmpeg are still running)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013年09月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
CRTMP视频直播服务器部署及测试
svn co –username anonymous –password “” https://svn.rtmpd.com/crtmpserver/trunk crtmpserver
全栈程序员站长
2022/06/27
1.2K0
农民工和黑客是如何挖断光纤的?吐槽球一样的流媒体日志系统
客服接到客户投诉,为毛直播看不了?为毛一直在卡?比较常见的答案是:农民工又挖断光纤了,黑客又攻击系统了。有毛线办法?看过FMS日志的都知道,里面没有任何有用的信息。 2012年我在某CDN时,就下决心再也不给农民工朋友摸黑,也不动用黑客攻击的大名。光纤挖断是有的,黑客攻击也是有的,问题是光纤没有断,黑客没有光顾时,卡和流不能看的概率也很大,服务器日志能给予原因吗?不能,这就是为何FMS日志,或其他不能告知原因的日志,球一样的原因了。 CDN会用到多台服务器,或者说是服务器集群,再加上调度那就是多个集群之间的
Winlin
2022/03/18
3870
crtmpserver流媒体服务器的介绍与搭建
Adobe的FMS(Flash Media Server)是很好用。但对应着分级授权的是money和有限功能开放。商业的东西既然用不起,也阻碍了我们的技术进步,那就只能求助于开源社区了。而crtmpserver就是FMS的替代者之一
雪影
2019/05/26
2.6K0
iOS关于直播 - 搭建服务端解析
其实本人一直都想自己简单做一套直播(包括移动端和服务端)的开发测试,但是之前一直做得比较迷茫。最近偶然间来了灵感,瞬间解除了我很多疑惑。
CC老师
2022/01/11
6170
iOS关于直播 - 搭建服务端解析
IPC 移动端或web端接入方案的RTMP实践
IPC出来的码流都是RTP码流,可能是裸的H264,也可能是PS流。如果要推流的话,有2种方案可以选择
用户4148957
2022/06/14
9120
IPC 移动端或web端接入方案的RTMP实践
如何开发一款 H5 小程序直播?
大前端这几年算是一个热词,对于前段来说如果不是大前端,技术相对来说就已经算是落后了。如果还停留在对ES6,Vue这些基本技能的学习只能说处于一个及格线。
Nealyang
2021/01/14
3.6K0
如何开发一款 H5 小程序直播?
【Java】Red5服务器搭建(实现在线直播,流媒体视频播放)
流媒体文件是目前非常流行的网络媒体格式之一,这种文件允许用户一边下载一边播放,从而大大减少了用户等待播放的时间。另外通过网络播放流媒体文件时,文件本身不会在本地磁盘中存储,这样就节省了大量的磁盘空间开销。正是这些优点,使得流媒体文件被广泛应用于网络播放。
小帅丶
2021/12/28
3.2K0
【Java】Red5服务器搭建(实现在线直播,流媒体视频播放)
【Java】Red5服务器搭建(实现在线直播,流媒体视频播放)「建议收藏」
流媒体文件是目前非常流行的网络媒体格式之一,这种文件允许用户一边下载一边播放,从而大大减少了用户等待播放的时间。另外通过网络播放流媒体文件时,文件本身不会在本地磁盘中存储,这样就节省了大量的磁盘空间开销。正是这些优点,使得流媒体文件被广泛应用于网络播放。
全栈程序员站长
2022/08/02
2.8K0
【Java】Red5服务器搭建(实现在线直播,流媒体视频播放)「建议收藏」
38款 流媒体服务器开源软件
更多Red5信息 最近更新: Red5 1.0.1 Final 发布,Flash流媒体服务器 发布于 12个月前
全栈程序员站长
2022/08/15
10.9K1
38款 流媒体服务器开源软件
RTSP?不存在的 -> 前端实时流探索记
传送门:https://segmentfault.com/a/1190000022994032
ConardLi
2020/07/07
3.2K0
RTMP服务器搭建(crtmpserver和nginx)简介
这里使用VLC播放器,下载VLC 开始播放,点击[媒体]->[流]->[网络] 输入刚刚推流的地址。然后选在下方的播放。
用户2929716
2018/08/23
3.3K0
RTMP服务器搭建(crtmpserver和nginx)简介
实时消息传输协议 RTMP(Real Time Messaging Protocol)
http://blog.csdn.net/defonds/article/details/17403225
bear_fish
2018/09/20
2.7K0
实时消息传输协议 RTMP(Real Time Messaging Protocol)
视频直播解决方案
当下,视频直播行业在中国逐渐走红。在刚刚过去的2015年,视频直播成为互联网行业最抢眼的领域之一。从游戏到秀场,从传统的网页端到移动互联网,各大直播平台包括斗鱼、熊猫tv、虎牙战旗还有纯移动端的印客、易直播等,群雄割据。言归正转,毕竟本文是一篇技术博客,接下来让我们从技术的角度分析如何搭建一个自己的直播平台。
全栈程序员站长
2022/09/15
1.4K0
视频直播解决方案
python+ngnix+ffmpeg+
安装python cd /opt yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils swig pyt
py3study
2020/01/08
1K0
html播放rtsp流,浏览器播放rtsp视频流解决方案
最近项目中需要实时播放摄像头rtsp视频流,于是就专门做了些研究。而浏览器不能直接播放,只有通过插件或者转码来实现这个需求。
全栈程序员站长
2022/11/02
6.2K0
FFmpeg推流到Nginx并使用播放器播放
如今直播很火,下面就简单分享下我是如何直播的。必备工具:FFmpeg,Nginx,还有一个播放器。之前在实例解析中分享过如何用FreeSWITCH来做直 播,这次分享下如何用FFmpeg配合Nginx做直播。
杜金房
2020/12/21
1.9K0
最简单的基于Flash的流媒体示例:RTMP推送和接收(ActionScript)
=====================================================
smy
2019/02/13
2K0
最简单的基于Flash的流媒体示例:RTMP推送和接收(ActionScript)
rtsp流媒体简单实践
本文主要记录如何通过ffmpeg实现监控视频的各种转换实现拉流推流。其中Onvif的应用在底部github代码中自行获取
leon公众号精选
2022/09/01
2.6K0
ubuntu搭建推流服务器Nginx+rtmp
****前言**** 最近这两年直播平台及其流行,然而我呢? 也要玩玩推流服务器~~~ 实现服务器推流/PC客户端观看/浏览器客户端查看 ---- ****简介**** 对于Nginx的优点呢就不多说了,两句话: 1)并发量高 2)可负载均衡 重点谈谈rtmp吧! RTMP全称是Real Time Messaging Protocol(实时消息传输协议),rmtp是一种通讯协议。该协议基于TCP,是一个协议族,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。RTMP是一种
AlicFeng
2018/06/08
4.1K0
Linux下视频流媒体服务器搭建详解「建议收藏」
用于支持培训网站中视频点拨功能,在培训网站总体方案中需要加入流媒体服务器,用于存储和传输视频资源。
全栈程序员站长
2022/11/01
7.8K0
Linux下视频流媒体服务器搭建详解「建议收藏」
相关推荐
CRTMP视频直播服务器部署及测试
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验