You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下使用FFmpeg混合两路麦克风音频与摄像头视频生成双音轨视频求助

Solution for Merging Two Microphone Audio Tracks with Camera Video using FFmpeg

Got it, let's get this sorted for you! To create an MP4 with your camera video plus two separate audio tracks (one from each microphone), you'll need to specify both audio inputs explicitly and use FFmpeg's -map option to ensure both tracks are included in the output.

Full Working Command

ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 \
-f alsa -ac 1 -thread_queue_size 512 -i hw:1,0 \
-f alsa -ac 1 -thread_queue_size 512 -i hw:2,0 \
-c:v libx264 -preset medium -crf 23 \
-c:a:0 libmp3lame -ab:0 96k \
-c:a:1 libmp3lame -ab:1 96k \
-map 0:v -map 1:a -map 2:a \
camera_with_two_audio_tracks.mp4

Breakdown of Each Part

Let's walk through what each section does so you understand how to adjust it for your setup:

  • Video Input: The first line is identical to your original command—grabbing 320x240 video from /dev/video0 via video4linux2.
  • Dual Audio Inputs: The next two lines are your two microphones. Replace hw:2,0 with the ALSA device ID of your second microphone. To find this, run arecord -l in your terminal—it will list all audio capture devices (look for lines like card 2: USB [USB Audio Device], device 0: USB Audio [USB Audio] which translates to hw:2,0).
  • Encoding Settings:
    • -c:v libx264 -preset medium -crf 23: I added this to use the widely supported H.264 video codec (your original command didn't specify a video codec, which could lead to compatibility issues). Adjust preset (faster = smaller file, slower = better quality) and crf (lower = better quality, 23 is a good default) as needed.
    • -c:a:0 and -c:a:1: These specify the codec for each audio track (libmp3lame, same as your original) and set their bitrates to 96k.
  • Mapping Tracks: The -map flags are critical here:
    • -map 0:v: Use the video stream from the first input (your camera)
    • -map 1:a: Use the audio stream from the second input (first microphone)
    • -map 2:a: Use the audio stream from the third input (second microphone)
      This ensures both audio tracks are included as separate streams in the output MP4.

Important Tips

  • Audio Synchronization: The -thread_queue_size 512 flags help prevent audio/video desync by increasing the input buffer size—this is especially useful for ALSA devices that might have small buffers.
  • Verify the Output: After running the command, check that both tracks exist with ffprobe camera_with_two_audio_tracks.mp4. You'll see two audio streams listed (e.g., Stream #0:1 and Stream #0:2) with their respective codec info.
  • Troubleshooting Device IDs: If you get "No such file or directory" errors for the ALSA devices, double-check the output of arecord -l to confirm the correct hw:X,Y path.

内容的提问来源于stack exchange,提问作者KingWealth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:13:54