使用FFmpeg同时录制屏幕与扬声器音频遇卡顿不同步问题求助
FFmpeg录制屏幕+扬声器音频卡顿、音视频不同步问题解决
问题现象
使用FFmpeg录制屏幕与扬声器音频时,输出视频卡顿严重且音视频不同步,同时收到以下警告:
[x11grab @ 0x563635302480] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8) [aac @ 0x563635319f40] Queue input is backward in time04.49 bitrate= 466.9kbits/s dup=268 drop=45 speed=1.84x [mp4 @ 0x5636353172c0] Non-monotonous DTS in output stream 0:1; previous: 217669, current: 210387; changing to 217670. This may result in incorrect timestamps in the output file.
但切换为ALSA麦克风源录制时,视频无异常。
原使用命令:
ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0 -f pulse -ac 2 -i alsa_output.pci-0000_2d_00.4.analog-stereo.monitor output.mp4
解决步骤
1. 提升输入线程队列大小
原命令中thread_queue_size默认值仅为8,高帧率录制时极易出现队列阻塞,进而引发丢帧、时间戳混乱问题。在每个输入流前添加-thread_queue_size参数,建议设置为1024或更高。
2. 强制同步时间戳
给视频和音频输入添加-use_wallclock_as_timestamps 1参数,强制基于系统时钟生成时间戳,从根源解决DTS非单调问题,保证音视频时间轴对齐。
3. 优化编码器配置
指定高效编码器并调整参数,避免编码速度跟不上录制速度导致的卡顿:
- 使用
libx264编码器(兼容性与性能更优) - 添加
-preset fast平衡编码速度与质量(追求极致速度可改用-preset ultrafast,画质略有下降) - 调整视频比特率(如
-b:v 5M),避免低比特率引发的编码压力与画质问题
4. 验证PulseAudio监听设备
若问题仍存在,可尝试改用PulseAudio默认监听源default.monitor替换命令中具体的设备名,规避设备识别异常导致的问题。
修改后的命令示例
ffmpeg -thread_queue_size 1024 -video_size 1920x1080 -framerate 60 -f x11grab -use_wallclock_as_timestamps 1 -i :0.0 \ -thread_queue_size 1024 -f pulse -ac 2 -use_wallclock_as_timestamps 1 -i alsa_output.pci-0000_2d_00.4.analog-stereo.monitor \ -c:v libx264 -preset fast -b:v 5M output.mp4
额外建议
如果硬件性能有限,可尝试将录制帧率从60fps降至30fps,降低系统处理压力,进一步减少卡顿概率。
内容的提问来源于stack exchange,提问作者dems98
相关产品推荐
相关产品推荐

