如何使用 FFMPEG 从具有不同音频源的网络摄像头录制 mp4 视频?

如何使用 FFMPEG 从具有不同音频源的网络摄像头录制 mp4 视频?

我正在尝试使用此命令,但结果是滞后,有时会停止,并且音频在 1 秒后消失。

音频 + 视频 - 不起作用

ffmpeg -f alsa -i hw:0,0 -f v4l2 -input_format mjpeg -i /dev/video0 -t 5 -strict -2 /var/www/html/tmp/temp.mp4 2>&1

如果我将音频和视频录制分开,效果就很好了。

视频 - 作品

ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -t 5 -strict -2 /var/www/html/tmp/temp.mp4 2>&1

音频 - 作品

ffmpeg -f alsa -i hw:0,0 /var/www/html/tmp/temp.mp3 2>&1

我的控制台输出:

ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
  configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, alsa, from 'hw:0,0':
  Duration: N/A, start: 1540888842.634797, bitrate: 1536 kb/s
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
[mjpeg @ 0x2624fc0] Changeing bps to 8
Input #1, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 338379.214188, bitrate: N/A
    Stream #1:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 800x600, -5 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
No pixel format specified, yuvj422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x261ba40] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x261ba40] profile High 4:2:2, level 3.1, 4:2:2 8-bit
[libx264 @ 0x261ba40] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - 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=6 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, mp4, to '/var/www/html/tmp/temp.mp4':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuvj422p(pc), 800x600, q=-1--1, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc56.60.100 libx264
    Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc56.60.100 aac
Stream mapping:
  Stream #1:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
  Stream #0:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[alsa @ 0x26095c0] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[alsa @ 0x26095c0] ALSA buffer xrun.
[alsa @ 0x26095c0] ALSA buffer xrun. 137kB time=00:00:02.14 bitrate= 525.9kbits/s    
[alsa @ 0x26095c0] ALSA buffer xrun. 185kB time=00:00:04.24 bitrate= 356.7kbits/s    
frame=  150 fps= 27 q=-1.0 Lsize=     266kB time=00:00:04.93 bitrate= 441.0kbits/s    
video:256kB audio:6kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.313912%
[libx264 @ 0x261ba40] frame I:1     Avg QP:24.07  size: 34967
[libx264 @ 0x261ba40] frame P:38    Avg QP:24.04  size:  4175
[libx264 @ 0x261ba40] frame B:111   Avg QP:25.53  size:   613
[libx264 @ 0x261ba40] consecutive B-frames:  1.3%  0.0%  0.0% 98.7%
[libx264 @ 0x261ba40] mb I  I16..4: 19.9% 59.7% 20.4%
[libx264 @ 0x261ba40] mb P  I16..4:  2.3%  1.3%  0.0%  P16..4: 74.9%  3.1%  5.5%  0.0%  0.0%    skip:12.9%
[libx264 @ 0x261ba40] mb B  I16..4:  0.1%  0.1%  0.0%  B16..8: 18.7%  0.1%  0.0%  direct: 0.8%  skip:80.2%  L0:65.7% L1:33.1% BI: 1.1%
[libx264 @ 0x261ba40] 8x8 transform intra:47.2% inter:91.3%
[libx264 @ 0x261ba40] coded y,uvDC,uvAC intra: 29.6% 67.7% 14.6% inter: 3.9% 18.6% 0.7%
[libx264 @ 0x261ba40] i16 v,h,dc,p: 28% 12%  6% 54%
[libx264 @ 0x261ba40] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 22% 39%  3%  3%  2%  5%  3%  4%
[libx264 @ 0x261ba40] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 26% 36%  7%  3%  4%  4% 12%  3%  4%
[libx264 @ 0x261ba40] i8c dc,h,v,p: 47% 19% 29%  5%
[libx264 @ 0x261ba40] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x261ba40] ref P L0:  8.8%  4.4% 83.1%  3.7%
[libx264 @ 0x261ba40] ref B L0: 14.5% 39.9% 45.6%
[libx264 @ 0x261ba40] ref B L1: 29.6% 70.4%
[libx264 @ 0x261ba40] kb/s:418.56

答案1

我必须将 alsa 改为 pulse 才能使其工作。

ffmpeg -f pulse -ac 2 -i default -f v4l2 -i /dev/video0 -t 00:00:18 -vcodec libx264 -strict -2 /var/www/html/tmp/temp.mp4

答案2

正如 Coron4extra 所提到的,您可能需要从 切换alsapulse。但对我来说,这没有帮助。

ffmpeg -f pulse ...

当我创建命令行时,我首先从视频开始,因为那是我当时所需要的全部。因此,当我尝试添加音频时,我确实编写了命令行(经过数小时的研究...但它仍然不起作用!)

ffmpeg \
    -f video4linux2 -s 1920x1080 -r 5 -re -i /dev/video0 \
         -input_format mjpeg -vcodec libx264 -bf 1 \
    -f alsa -ac 2 -i hw:CARD=C930e,DEV=0 -acodec mp2 \
    http://localhost:8090/wall.ffm

因此命令行首先设置视频(前两行),然后设置alsa音频。最后我将输出定义为 ffm(即 FFMPEG 服务器)。

这会给我一个我无法理解的错误:

输入 #0,video4linux2,v4l2,来自‘/dev/video0’:
持续时间:N/A,开始:27865.911813,比特率:165888 kb/s
流 #0:0:视频:rawvideo(YUY2 / 0x32595559),yuyv422,1920x1080,165888 kb/s,5 fps,5 tbr,1000k tbn,1000k tbc
未知解码器‘libx264’

解决方案是什么?在命令行上交换音频和视频参数,就成功了!

ffmpeg \
    -f alsa -ac 2 -i hw:CARD=C930e,DEV=0 -acodec mp2 \
    -f video4linux2 -s 1920x1080 -r 5 -re -i /dev/video0 \
        -input_format mjpeg -vcodec libx264 -bf 1 \
    http://localhost:8090/wall.ffm

是的,参数完全一样。只需将音频放在视频前面,它就可以按预期工作了。

相关内容