如何使用 ffmpeg 将视频从网络摄像头流式传输到网络?

如何使用 ffmpeg 将视频从网络摄像头流式传输到网络?

我正在尝试从我的 Logitech C920 网络摄像头流式传输 h264 视频。我正在使用这样的 ffserver.conf:

Port 8099

NoDaemon

BindAddress 0.0.0.0


RTSPPort 5004
RTSPBindAddress 0.0.0.0

MaxClients 10

MaxBandwidth 10000

CustomLog -


<Feed feed1.ffm>
        File /tmp/feed1.ffm
        FileMaxSize 20M
</Feed>

<Stream viewport1>
        Feed feed1.ffm
        Format rtp
        VideoCodec libx264
        VideoFrameRate 15
        VideoBufferSize 40
        VideoBitRate 3000
        VideoQMin 1
        VideoQMax 31
        VideoSize 640x480
        PreRoll 0
        NoAudio
        Strict -1
</Stream>

<Stream stat.html>
        Format status
</Stream>

<Redirect index.html>
        URL http://ffmpeg.sourceforge.net/
</Redirect>

我像这样启动 ffmpeg:

./ffmpeg -f video4linux2 -s 640x480 -r 15 -re -i /dev/video0 -an -vcodec libx264  -bf 5  http://localhost:8099/feed1.ffm

但输出中有:

[video4linux2,v4l2 @ 0xc0b5e0] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 2930.711051, bitrate: 73728 kb/s
    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 73728 kb/s, 15 tbr, 1000k tbn, 15 tbc
[libx264 @ 0xc183f0] using cpu capabilities: ARMv6 NEON
[libx264 @ 0xc183f0] VBV buffer size cannot be smaller than one frame, using 400 kbit
[libx264 @ 0xc183f0] profile High 4:2:2, level 2.2, 4:2:2 8-bit
[libx264 @ 0xc183f0] 264 - core 120 r2151 a3f4407 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=0 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=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=5 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=1 weightp=2 keyint=50 keyint_min=5 scenecut=0 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=3000 ratetol=1.0 qcomp=0.50 qpmin=10 qpmax=51 qpstep=4 vbv_maxrate=6000 vbv_bufsize=400 nal_hrd=none ip_ratio=1.40 aq=1:1.00
Output #0, ffm, to 'http://localhost:8099/feed1.ffm':
  Metadata:
    creation_time   : now
    encoder         : Lavf54.44.100
    Stream #0:0: Video: h264, yuv422p, 640x480, q=10-51, 3000 kb/s, 1000k tbn, 15 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
[libx264 @ 0xc183f0] VBV buffer size cannot be smaller than one frame, using 400 kbit
frame=   12 fps=0.0 q=0.0 size=       4kB time=00:00:00.00 bitrate=   0.0kbits/sframe=   22 fps= 20 q=0.0 size=       4kB time=00:00:00.00 bitrate=   0.0kbits/sframe=   30 fps= 19 q=0.0 size=       4kB time=00:00:00.00 bitrate=   0.0kbits/sframe=   38 fps= 18 q=0.0 size=       4kB time=00:00:00.00 bitrate=   0.0kbits/s

有什么建议如何不进行转码并直接流式传输视频吗?

答案1

实际上,我已经成功使用 ffmpeg 流式传输 h264 视频。我能够在 ffmpeg 用户列表的帮助下做到这一点,特别是来自这个补丁的作者 Carl Eugen Hoyos 的帮助:

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c 
index cd6aeb2..c3f813d 100644 
--- a/libavdevice/v4l2.c 
+++ b/libavdevice/v4l2.c 
@@ -150,6 +150,7 @@ static struct fmt_map fmt_conversion_table[] = { 
 { AV_PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 }, 
 { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG }, 
 { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG }, 
+ { AV_PIX_FMT_NONE, AV_CODEC_ID_H264, V4L2_PIX_FMT_H264 }, 
 #ifdef V4L2_PIX_FMT_CPIA1 
 { AV_PIX_FMT_NONE, AV_CODEC_ID_CPIA, V4L2_PIX_FMT_CPIA1 }, 
 #endif

我使用过的实际命令:

./ffmpeg -f video4linux2 -s 640x480 -r 15 -vcodec h264 -i /dev/video0 -an http://localhost:8099/feed1.ffm

但我无法摆脱转码,所以我改用了 vlc (并成功了)。

答案2

尝试输出流上的复制选项-codec copy

./ffmpeg -f video4linux2 -s 640x480 -r 15 -vcodec h264 -i /dev/video0 -codec copy -an http://localhost:8099/feed1.ffm

这会带来好坏参半的结果。我可以使用vcodec设置进行复制,但即使 Logitech HD920 支持两者,也mjpeg无法进行复制。h264复制选项大大地降低 CPU 利用率。

附言。使用最新版本,因为早期版本中的副本可能已损坏。

相关内容