将 WEBM 转码为 RTMP

问题描述 投票:0回答:2

总结

我的目标是从浏览器获取网络摄像头流并将其输入名为 Restreamer 的程序中,该程序接收 RTMP 流。

我推断浏览器Recording API生成可以保存为WEBM的Blob。

为了将 WEBM 内容流式传输到 Restreamer,我尝试使用 FFmpeg。我读过强制 FLV 格式是必需的,但除此之外所有的论点对我来说都是希腊语。我一直无法找到有人尝试从 WEBM 转到 RTMP 的类似主题。我找到了反其道而行之的例子,但事实证明反转 FFmpeg 命令没有效果。

保存到FLV效果很好。使用以下命令,我可以将 WEBM 文件转码为 FLV 并在 VLC 上播放:

ffmpeg -i ~/big-buck-bunny_trailer.webm -f flv out.flv

但是,如果不是输出到文件,而是将其传递给 RTMP,我会得到以下输出:

ffmpeg -i ~/Downloads/big-buck-bunny_trailer.webm -f flv "rtmp://example.com/live"

ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12 (GCC)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --extra-ldflags='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 ' --extra-cflags=' -I/usr/include/rav1e' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --enable-chromaprint --disable-crystalhd --enable-fontconfig --enable-frei0r --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libaom --enable-libdav1d --enable-libass --enable-libbluray --enable-libbs2b --enable-libcdio --enable-libdrm --enable-libjack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libmysofa --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librav1e --enable-librtmp --enable-librubberband --enable-libsmbclient --enable-version3 --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-version3 --enable-vapoursynth --enable-libvpx --enable-vulkan --enable-libglslang --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libxml2 --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-avfilter --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-lto --enable-libmfx --enable-runtime-cpudetect
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
Input #0, matroska,webm, from '/home/kyjus25/big-buck-bunny_trailer.webm':
  Metadata:
    encoder         : http://sourceforge.net/projects/yamka
    creation_time   : 2010-05-20T08:21:12.000000Z
  Duration: 00:00:32.48, start: 0.000000, bitrate: 533 kb/s
  Stream #0:0(eng): Video: vp8, yuv420p(progressive), 640x360, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn (default)
  Stream #0:1(eng): Audio: vorbis, 44100 Hz, mono, fltp (default)
HandShake: client signature does not match!
Stream mapping:
  Stream #0:0 -> #0:0 (vp8 (native) -> flv1 (flv))
  Stream #0:1 -> #0:1 (vorbis (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, flv, to 'rtmp://example.com/live':
  Metadata:
    encoder         : Lavf59.16.100
  Stream #0:0(eng): Video: flv1 ([2][0][0][0] / 0x0002), yuv420p(tv, bt470bg/unknown/unknown, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 1k tbn (default)
    Metadata:
      encoder         : Lavc59.18.100 flv
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
  Stream #0:1(eng): Audio: mp3 ([2][0][0][0] / 0x0002), 44100 Hz, mono, fltp (default)
    Metadata:
      encoder         : Lavc59.18.100 libmp3lame
WriteN, RTMP send error 32 (136 bytes)7kB time=00:00:00.39 bitrate= 136.7kbits/s speed=71.2x    
WriteN, RTMP send error 32 (35 bytes)
WriteN, RTMP send error 9 (42 bytes)
av_interleaved_write_frame(): Operation not permitted
    Last message repeated 1 times
[flv @ 0x55d0dd0af700] Failed to update header with correct duration.
[flv @ 0x55d0dd0af700] Failed to update header with correct filesize.
Error writing trailer of rtmp://example.com/live: Operation not permitted
frame=   53 fps=0.0 q=4.3 Lsize=     146kB time=00:00:02.45 bitrate= 486.8kbits/s speed=42.8x    
video:128kB audio:19kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Error closing file rtmp://example.com/live: Operation not permitted
Conversion failed!

这里有几个有趣的兔子洞可供追踪,但在追踪了所有这些之后我什么也没发现。

HandShake: client signature does not match!
:
我认为这更多是警告而不是错误,因为我是从“WEBM -> FLV”而不是更传统的“MP4 -> FLV”开始。

av_interleaved_write_frame(): Operation not permitted
:
我在这方面发现了几个问题。其中一个称其为“存储问题”,另一个称其为“文件权限问题”。我有足够的磁盘空间,并尝试将输入文件设置为 777 权限。然而,我发现的文件权限问题的示例都涉及输出到文件而不是 RTMP IP。 Failed to update header with correct duration:

我发现的建议

是将
-flvflags no_duration_filesize
添加到命令中,这确实会抑制“更新失败...”错误,但不能解决总体问题。 我尝试过的事情
• 多格式转码

MP4 到 RTMP 工作正常:

ffmpeg -i ~/Downloads/big-buck-bunny_trailer.mp4 -f flv "rtmp://example.com/live"

理论上,我可以将 WEBM 流式传输到文件,将其转码为 MP4 文件,然后将其转码为 FLV/RTMP。听起来很糟糕。

• 支付服务费用(Wowza、Flashphoner、api.video 等)

不幸的是,这正是我想要避免的。

• WebRTC 转 RTMP?

WebRTC 似乎是一种点对点连接,并且不能很好地适应服务器/客户端场景。

• WebRTC 到其他摄取格式

Restreamer 还支持 HLS、DASH、RTP、RTSP、RTMP 和 SRT 的传入流。然而,这些似乎都是通过 IP URL 公开的网络源的示例。我不确定 FFmpeg 能做到这一点。

• 使用 ffmpeg-wasm 代替 CLI

可用

here

,我想也许通过使用浏览器实现我可能会得到不同的结果。但不是。甚至控制台都没有错误。

• 来自 OBS 的流媒体 郑重声明,是的,我尝试过从 OBS 进行流式传输,而不是通过 FFmpeg,并且 Restreamer 平台本身确实可以正常使用。我经常用它。

帖子脚本

我在网上找不到任何相关的解决方案。令我震惊的是,之前 1000 次都没有解决来自浏览器网络摄像头的流传输问题。这与 7 年前提出的

问题

有关,但尚未解决,并且 Flash

不再是一个选项

推荐 • 添加

-c:v libx264 -flags:v +global_header -c:a aac -ac 2

:

ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12 (GCC)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --extra-ldflags='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 ' --extra-cflags=' -I/usr/include/rav1e' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --enable-chromaprint --disable-crystalhd --enable-fontconfig --enable-frei0r --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libaom --enable-libdav1d --enable-libass --enable-libbluray --enable-libbs2b --enable-libcdio --enable-libdrm --enable-libjack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libmysofa --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librav1e --enable-librtmp --enable-librubberband --enable-libsmbclient --enable-version3 --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-version3 --enable-vapoursynth --enable-libvpx --enable-vulkan --enable-libglslang --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libxml2 --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-avfilter --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-lto --enable-libmfx --enable-runtime-cpudetect
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
Input #0, matroska,webm, from '/home/kyjus25/big-buck-bunny_trailer.webm':
  Metadata:
    encoder         : http://sourceforge.net/projects/yamka
    creation_time   : 2010-05-20T08:21:12.000000Z
  Duration: 00:00:32.48, start: 0.000000, bitrate: 533 kb/s
  Stream #0:0(eng): Video: vp8, yuv420p(progressive), 640x360, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn (default)
  Stream #0:1(eng): Audio: vorbis, 44100 Hz, mono, fltp (default)
HandShake: client signature does not match!
Stream mapping:
  Stream #0:0 -> #0:0 (vp8 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (vorbis (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x561564271fc0] using SAR=1/1
[libx264 @ 0x561564271fc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x561564271fc0] profile High, level 3.0, 4:2:0, 8-bit
[libx264 @ 0x561564271fc0] 264 - core 163 r3060 5db6aa6 - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://example.com/live':
  Metadata:
    encoder         : Lavf59.16.100
  Stream #0:0(eng): Video: h264 ([7][0][0][0] / 0x0007), yuv420p(tv, bt470bg/unknown/unknown, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 1k tbn (default)
    Metadata:
      encoder         : Lavc59.18.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1(eng): Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      encoder         : Lavc59.18.100 aac
Larger timestamp than 24-bit: 0xffffff77kB time=00:00:30.18 bitrate= 460.0kbits/s speed=19.7x    
[flv @ 0x56156425e440] Failed to update header with correct duration.
[flv @ 0x56156425e440] Failed to update header with correct filesize.
frame=  812 fps=475 q=-1.0 Lsize=    1901kB time=00:00:32.52 bitrate= 479.0kbits/s speed=  19x    
video:1354kB audio:508kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.123872%
[libx264 @ 0x561564271fc0] frame I:21    Avg QP:15.05  size:  8839
[libx264 @ 0x561564271fc0] frame P:293   Avg QP:20.78  size:  3087
[libx264 @ 0x561564271fc0] frame B:498   Avg QP:22.20  size:   593
[libx264 @ 0x561564271fc0] consecutive B-frames: 14.7%  7.6%  9.2% 68.5%
[libx264 @ 0x561564271fc0] mb I  I16..4: 42.6% 41.9% 15.5%
[libx264 @ 0x561564271fc0] mb P  I16..4:  4.0%  8.0%  0.9%  P16..4: 22.7%  8.0%  4.0%  0.0%  0.0%    skip:52.4%
[libx264 @ 0x561564271fc0] mb B  I16..4:  1.4%  1.5%  0.2%  B16..8: 12.0%  1.0%  0.2%  direct: 3.3%  skip:80.5%  L0:44.1% L1:47.1% BI: 8.8%
[libx264 @ 0x561564271fc0] 8x8 transform intra:53.6% inter:57.0%
[libx264 @ 0x561564271fc0] coded y,uvDC,uvAC intra: 35.1% 37.7% 11.2% inter: 7.8% 9.5% 2.4%
[libx264 @ 0x561564271fc0] i16 v,h,dc,p: 56% 21% 14%  9%
[libx264 @ 0x561564271fc0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 26% 21%  3%  3%  4%  4%  3%  4%
[libx264 @ 0x561564271fc0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 21% 18%  4%  7%  7%  6%  5%  4%
[libx264 @ 0x561564271fc0] i8c dc,h,v,p: 62% 21% 15%  2%
[libx264 @ 0x561564271fc0] Weighted P-Frames: Y:20.8% UV:19.5%
[libx264 @ 0x561564271fc0] ref P L0: 70.3% 17.7%  9.0%  2.9%  0.0%
[libx264 @ 0x561564271fc0] ref B L0: 89.3%  8.9%  1.9%
[libx264 @ 0x561564271fc0] ref B L1: 96.4%  3.6%
[libx264 @ 0x561564271fc0] kb/s:341.30
[aac @ 0x561564223140] Qavg: 952.636

看似成功完成,但速度相当快。日志输出一个新的

Larger timestamp than 24-bit: 0xffffff77kB

    

能够通过使用使其工作:

ffmpeg video-streaming webcam rtmp webm
2个回答
0
投票
ffmpeg -re -i sample.webm -c:v h264 -c:a aac -f flv "rtmp://example.com/live"

-re
对于使其实时流式传输至关重要,否则@Gyan的建议也会起作用!

你好,我也尝试将网络摄像头视频发送到 rtmplink 但没有成功。尝试使用 ffmpeg 录制小视频。因此,在录制成功后,我尝试使用 webrtc 中的前端媒体录制并将其数据发送到服务器,我在其中使用 ffmpeg 来使用数据并发送到 rtmp 链接,但无法像 restreamer 那样将数据发送到 rtmp 服务器。


0
投票

视频:真实}); const mediaRecorder = new MediaRecorder(videoStream, { mimeType: 'video/webm' });

            mediaRecorder.ondataavailable = async (event) => {
                if (event.data.size > 0) {
                    const arrayBuffer = await event.data.arrayBuffer();
                    const uint8Array = new Uint8Array(arrayBuffer);
    
                    // Convert Uint8Array to regular array of integers
                    const bufferArray = Array.from(uint8Array);
    
                    const dataToSend = {
                        buffer: bufferArray,
                        mode: "SendRtmp"
                    };
    
                    ws.send(JSON.stringify(dataToSend));
                }
            };
    
            mediaRecorder.start(1000); // Send data every 1 second
        }
    };

in server.js  

    const SendRtmpVideo = (buffer) => {
    console.log("data",buffer);
    const ffmpegCommand = [
        '-re',
        '-f', 'rawvideo',
        '-pix_fmt', 'yuv420p',
        '-s', '640x480', // Update with your video frame size
        '-i', 'pipe:0',  // Input from stdin
        '-c:v', 'libx264',
        '-loglevel', 'debug', // Set log level to debug
        '-preset', 'ultrafast',
        '-tune', 'zerolatency',
        '-b:v', '3000k',
        '-bufsize', '3000k',
        '-maxrate', '3000k',
        '-g', '60',
        '-c:a', 'aac',
        '-b:a', '128k',
        '-f', 'flv',
        RTMPLINK
    ];
    

    let ffmpeg = spawn('ffmpeg', ffmpegCommand);

    ffmpeg.on('exit', (code, signal) => {
        if (code === 0) {
            console.log('FFmpeg process exited successfully.');
        } else {
            console.error(`FFmpeg process exited with code ${code} and signal ${signal}`);
        }
    });

    ffmpeg.on('error', (error) => {
        console.error('FFmpeg error:', error);
    });

    
    ffmpeg.stdout.on('data', (data) => {
        console.log(`FFmpeg stdout: ${data}`);
    });
    
    ffmpeg.stderr.on('data', (data) => {
        console.error(`FFmpeg stderr: ${data}`);
    });
    ffmpeg.stdin.write(buffer, (err) => {
        if (err) {
            console.error('Error writing to FFmpeg stdin:', err);
        } else {
            console.log('Data written to FFmpeg stdin successfully.');
        }
    });
};

    wss.on('connection', (ws) => {
    console.log('WebSocket connection established.');

    ws.on('message', (data) => {
        try {
            const parsedData = JSON.parse(data);

        if (parsedData && typeof parsedData === 'object') {
            const { buffer, mode } = parsedData;

            if (mode === "SendRtmp" && buffer instanceof Array) {
                // Convert the buffer array to Uint8Array and then to Buffer
                const bufferArray = Buffer.from(new Uint8Array(buffer));
                SendRtmpVideo(bufferArray);
            } else {
                console.log("Received unknown or invalid mode or buffer data");
            }
        } else {
            console.log("Received unknown or invalid JSON data");
        }
    } catch (error) {
        console.error('Error parsing WebSocket message:', error);
    }
});

    ws.on('close', () => {
        console.log('WebSocket connection closed.');
    });
});

// Start the HTTP server
const PORT = 3001;
server.listen(PORT, () => {
    console.log(`WebSocket server listening on port ${PORT}`);
});  

© www.soinside.com 2019 - 2024. All rights reserved.