简体   繁体   中英

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. I then grab that on a node server, use createWriteStream to set up a pipe and start streaming!

I'm stuck at converting the media from webm to a live m3u8. Below is the ffmpeg command I'm running (It's been through numerous iterations as I've tried things from the docs).

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.

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?

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. My code is on Github , and its hosted at livestream.streamclarity.com (and is a WIP).

If I run my node server locally, and take the stream from the browser - FFMPEG never crashes and runs forever. 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). 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. My fix was to constrain the video coming from the camera, so all FFMPEG has to do is change the format.

Other FFMPEG options to consider: (to replace CRF 51) -preset ultrafast, -tune zerolatency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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