简体   繁体   English

在HTML页面中播放h.264视频流

[英]Play h.264 video stream in html page

I am using ffmpeg and vlc on linux to produce MPEG transport stream ( mpegts ) over HTTP. 我在Linux上使用ffmpegvlc来通过HTTP产生MPEG传输流( mpegts )。 Basically ffmpeg captures the screen and produces h.264 stream using mpegts and then vlc is used as a server that delivers the stream over http . 基本上ffmpeg捕获屏幕并使用mpegts生成h.264流,然后vlc用作通过http传递流的服务器。 That's how I manage to accomplish this. 这就是我设法做到这一点的方法。

ffmpeg -f x11grab -s 1280x800 -r 30 -i :0.0+0,0  -f alsa -ac 2 -i pulse -vcodec libx264 -preset ultrafast -s 1280x800 -threads 0 -f mpegts - | vlc -I dummy - --sout '#std{access=http,mux=ts,dst=:3030}

And some useful log about the resulting stream: 还有一些有关结果流的有用日志:

Output #0, mpegts, to 'pipe:':
  Metadata:
    encoder         : Lavf57.72.101
    Stream #0:0: Video: h264 (libx264), yuv444p(progressive), 1280x800, q=-1--1, 30 fps, 90k tbn, 30 tbc
    Metadata:
      encoder         : Lavc57.96.101 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp2, 48000 Hz, stereo, s16, 384 kb/s
    Metadata:
      encoder         : Lavc57.96.101 mp2

All looks good except for that I am unable to play the stream in a HTML page. 除了无法在HTML页面中播放流之外,其他所有内容看起来都不错。 Could someone point me at some solution to either render the stream in HTML page or change the stream type so that it gets rendered. 有人可以向我指出一些解决方案,要么在HTML页面中呈现流,要么更改流类型以使其呈现。 Thank you in advance! 先感谢您!

EDIT: I've noticed something weird. 编辑:我注意到一些奇怪的事情。 The <video> fails to play mp4 files on Firefox (51.0.1) for Ubuntu (I have also tested on Windows 8 with Chrome). <video>无法在Ubuntu的Firefox(51.0.1)上播放mp4文件(我也已经在Windows 8和Chrome上进行了测试)。 I simply use ffmpeg to record screen for 10 seconds and produce .mp4 video (see below). 我只是使用ffmpeg录制屏幕10秒钟并生成.mp4视频(请参见下文)。

ffmpeg -video_size 1280x800 -framerate 25 -f x11grab -i :0.0+0,0 -t 00:00:10 ~/Workspace/videostream/output.mp4

And then play the output.mp4 video with the following snippet. 然后使用以下代码片段播放output.mp4视频。

<video width="480" height="320" controls="controls">
    <source src="output.mp4" type="video/mp4">
</video>

This simply does not play although I can open the video with any other media player. 尽管我可以使用任何其他媒体播放器打开视频,但它根本无法播放。 The media info (using MediaInfo, v0.7.96) of the output.mp4 file is: output.mp4文件的媒体信息(使用MediaInfo,v0.7.96)是:

GENERAL: MPEG-4 (Base Media): 1.007 MiB, 10s 0ms; 1 Video stream: AVC; Overall bit rate: 825 Kbps; Writing application: Lavf57.72.101
VIDEO: 2 343 Kbps, 1280*800(16:10), at 25.000 fps, MPEG Video (Version 2) (Main@High 1440)

However, if I replace the output.mp4 with any other .mp4 file, it does play in Firefox. 但是,如果将output.mp4替换为其他任何.mp4文件,则它确实可以在Firefox中播放。 So, this leads me to conclude that it is not a browser issue but something with the way I exploit ffmpeg . 因此,这使我得出结论,这不是浏览器问题,而是与我利用ffmpeg的方式有关的问题。 Below is the media info of a proper .mp4 file that successfully runs in the browser. 以下是在浏览器中成功运行的正确.mp4文件的媒体信息。

GENERAL: MPEG-4 (Base Media/Version 2): 48.4 MiB, 2mn 50s; 1 Video stream: AVC; 1 Audio stream: AAC; Overall bit mode rate: Variable; Overall bit rate: 2 385 Kbps;
VIDEO: 2 256 Kbps, 1280*720(16:9), at 25.000 fps, AVC (Main@L3.1) (CABAC / 3 Ref Frames); ISO media produced by Google Inc.
AUDIO:
126 Kbps, 44.1 KHz, 2 channels, AAC (LC), ISO media produced by Google Inc.

And here is the media info of the output file that results from redirecting the h.264 mpegts stream using the ffmpeg command with -pix_fmt yuv420p ./ffmpeg -t 00:00:10 -f x11grab -s 1280x800 -r 30 -i :0.0+0,0 -pix_fmt yuv420p -vcodec libx264 -preset ultrafast -s 1280x800 -threads 0 -f mpegts - > ~/Workspace/file.ts : 这是输出文件的媒体信息,它是通过使用ffmpeg命令和-pix_fmt yuv420p重定向h.264 mpegts流而得到的。/ffmpeg-t ./ffmpeg -t 00:00:10 -f x11grab -s 1280x800 -r 30 -i :0.0+0,0 -pix_fmt yuv420p -vcodec libx264 -preset ultrafast -s 1280x800 -threads 0 -f mpegts - > ~/Workspace/file.ts

GENERAL: MPEG-TS: 2.93 MiB, 9s 960ms; 1 Video stream: MPEG Video; 1 Menu stream: MPEG Video; Overall bit rate mode: Variable; Overall bit rate: 2 465 Kbps; 
VIDEO: 2 343 Kbps, 1280*800(16:10), at 25.000 fps, MPEG Video (Version 2) (Main@High 1440)

I don't think yuv444p is playable in the <video> tag by any browser. 我认为yuv444p不能在任何浏览器的<video>标记中播放。

You probably need an ffmpeg binary that can output the more mellow yuv420p planar. 您可能需要一个ffmpeg二进制文件,可以输出更柔和的yuv420p平面。 Maybe you can pass that as option to libx264 , like this: -pix_fmt yuv420p . 也许您可以将其作为选项传递给libx264 ,如下所示: -pix_fmt yuv420p

EDIT: Here's what worked for me: 编辑:这对我有用:

"C:\Program Files\VideoLAN\VLC\vlc.exe" screen:// :screen-fps=25 :screen-caching=5000 :sout=#transcode{vcodec=theo,vb=800,scale=1,width=800,height=600,acodec=none}:http{mux=ogg,dst=:8181/desktop} :no-sout-rtp-sap :no-sout-standard-sap :ttl=1 :sout-keep

The only downside seems to be the performance. 唯一的缺点似乎是性能。 It's slow. 太慢了 Maybe it's something specific to the Windows build of VLC, hopefully it does better on Linux. 也许这是Windows版本的VLC特有的,希望它在Linux上表现更好。 Also, try other codec/container combo, i have no clue if Theora is the appropriate choice for encoding a desktop capture. 另外,尝试其他编解码器/容器组合,我不知道Theora是否是编码桌面捕获的合适选择。

Tested in Chrome (Windows) with 在Chrome(Windows)中进行了测试

<video controls>
    <source src="http://127.0.0.1:8181/desktop">
</video>

NOTE: You don't have to use localhost, VLC listens on 注意:您不必使用localhost,VLC会监听

TCP    0.0.0.0:8181

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM