简体   繁体   English

使用 ffmpeg 创建实时更新视频 stream

[英]Creating a live, updating video stream with ffmpeg

I've set up a data stream from my webcam using the MediaSource api and set it to send data from my webcam in webm format, every 4 seconds.我已经使用MediaSource api 从我的网络摄像头设置了数据 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ,并将其设置为每 4 秒从我的网络摄像头以 webm 格式发送数据。 I then grab that on a node server, use createWriteStream to set up a pipe and start streaming!然后我在节点服务器上抓取它,使用createWriteStream设置 pipe 并开始流式传输!

I'm stuck at converting the media from webm to a live m3u8.我坚持将媒体从 webm 转换为live m3u8。 Below is the ffmpeg command I'm running (It's been through numerous iterations as I've tried things from the docs).下面是我正在运行的 ffmpeg 命令(它经历了无数次迭代,因为我尝试了文档中的内容)。

const cmd = `ffmpeg
    -i ${filepath}
    -profile:v baseline
    -level 3.0
    -s 640x360 -start_number 0
    -hls_time 10
    -hls_list_size 0
    -hls_flags append_list
    -hls_playlist_type event
    -f hls ${directory}playlist.m3u8`

  const ls = exec(cmd.replace(/(\r\n|\n|\r)/gm," "), (err, stdout, stderr) => {
    if(err) {
      console.log(error);
    }

  })

I can't remove the #EXT-X-ENDLIST at the end of the playlist, to keep the stream live for my web players, so when I hit play - the video plays the playlist in its current state and stops at the end.我无法删除播放列表末尾的#EXT-X-ENDLIST ,以保持 stream 为我的 web 播放器直播,所以当我点击播放时 - 视频播放当前 Z9ED39E2A631586B73EZ8 中的播放列表并在结束时停止。

Thanks谢谢

UPDATE更新

This may be a quality/speed issue.这可能是质量/速度问题。 When I reduced the quality down to;当我将质量降低到;

const cmd = `ffmpeg
    -i ${filepath}
    -vf scale=w=640:h=360:force_original_aspect_ratio=decrease
    -profile:v main
    -crf 51
    -g 48 -keyint_min 48
    -sc_threshold 0
    -hls_time 4
    -hls_playlist_type event
    -hls_segment_filename ${directory}720p_%03d.ts
    ${directory}playlist.m3u8

I was able to get a pixelated live video.我能够获得像素化的实时视频。 However, it quickly crashed... Maybe this is not possible in Node/Web Browsers yet?然而,它很快就崩溃了……也许这在 Node/Web 浏览器中是不可能的?

Matt,马特,

I am working on a similar project.我正在做一个类似的项目。 I am converting on NODE to FLV, and then using api.video to convert the FLV to HLS.我正在将 NODE 转换为 FLV,然后使用api.video将 FLV 转换为 HLS。 My code is on Github , and its hosted at livestream.streamclarity.com (and is a WIP).我的代码在Github上,它托管在livestream.streamclarity.com上(并且是 WIP)。

If I run my node server locally, and take the stream from the browser - FFMPEG never crashes and runs forever.如果我在本地运行我的节点服务器,并从浏览器中获取 stream - FFMPEG 永远不会崩溃并永远运行。 However, when it is hosted remotely, FFMPEG runs for a bit and then crashes - so I'm pretty sure the issue is the websocket (or perhaps my network).但是,当它被远程托管时,FFMPEG 会运行一段时间然后崩溃 - 所以我很确定问题是 websocket(或者可能是我的网络)。 Lowering the video size I upload to the server helps (for a bit).降低我上传到服务器的视频大小会有所帮助(有点)。

What I have found is any video rescaling, or audio processing that you do in FFMPEG adds a delay to the processing and tends to crash more.我发现您在 FFMPEG 中进行的任何视频重新缩放或音频处理都会增加处理延迟,并且往往会导致更多崩溃。 My fix was to constrain the video coming from the camera, so all FFMPEG has to do is change the format.我的解决方法是限制来自摄像机的视频,所以 FFMPEG 所要做的就是更改格式。

Other FFMPEG options to consider: (to replace CRF 51) -preset ultrafast, -tune zerolatency其他要考虑的 FFMPEG 选项:(替换 CRF 51)-preset ultrafast,-tune zerolatency

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

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