Audio desync while recording

I’m trying to figure out if it’s possible to record video and audio at the same time without getting desyncs. I use FFmpeg for recording video, and SoX for audio (it’s possible to use FFmpeg for audio and it usually doesn’t desync there, but it outputs only one file, while I need to record both system sounds and my microphone, and one video file with two audio streams is usually not recognised by players and when uploading to YouTube and such). Normally, I would record it with a piped command like this:

ffmpeg <options> Video.mp4 | sox <options> AudioS1.ogg | sox <options> AudioS2.ogg

That would work, but the problem is that it tends to desync, and a whole lot. I get the message “ALSA buffer xrun” a lot, and often times the desync is very dramatic, with the video file being 2:00 minutes long, while the audio files being around 1:30 and 1:45 minutes long (I haven’t tested it for a longer period of time, but I’d suspect the result would be even more dramatic).

Of course, the best way would be to find out just exactly what causes the desync in the first place… I am using openSUSE 12.1 without PulseAudio (SoX takes input directly from ALSA), and my primary sound card is a Creative X-Fi card (I do have an integrated sound card, too). What could cause the xruns in the first place? Also, is it better to output the files to my native Btrfs filesystem, or NTFS, which is on another hard drive, so is not heavily used at all?

And perhaps as a workaround it could be possible, instead of skipping audio, have the program insert silence there, so that at least the rest of the audio would be in sync with the video?

What kernel are you using? If it’s the default one that could be a problem. I mean the one that’s labelled “default”, as opposed to “desktop”, which might be the one that is installed by default by opensuse, I am not sure about that. You can check with “uname -r” from the command line, it should be “[bunch of numbers]-desktop”. If you’re using proprietary video drivers switching kernels can break them (or, from experience with ATI, switching kernels is guaranteed to break them.)

If that’s not the problem there are other ways to get better performance, adjusting runtime priority for one. I don’t think it would matter which hard drive is used for performance, so that is probably just a matter of preference. Oh metering cpu and memory might reveal something too, if either of those is maxing out, then there really is nothing really wrong, you just have to find a way to economize memory and clock cycles.

Also you can try running jack, might be a bit of overkill for just making a recording, but it might also toss an error that lets you know what the problem is. Getting the program “qjackctl” and hitting the play button (the big one, might be called start, not the little one for the transport) might toss an informative error. I think ardour has some good error messages too, but that is definitely overkill for what you are doing (though in all fairness, it is not as hard to use as it seems at first glance.)

Well, I guess I don’t know exactly, it could be a lot of things. I guess check the cpu meter first, then the kernel version, and then probably runtime priority, but fiddling with that is maybe not the easiest if you don’t already know how to do it. Maybe just installing jack and hitting play on qjackctl, and then trying to record in audacity (go to preferences and make it use jack, I think it connects itself up to your system automatically after that) is an easy thing to try. Jack is probably a dependancy of qjackctl.

Well, I guess I don’t know, hope something here is useful.

I’m using the desktop kernel.

Actually, I think I found the answer… As you can see, I recorded the sound in OGG earlier. I changed it to WAV instead, and now the outputs have the exact same length. The CPU usage when recording also went down considerably (now SoX uses only around 1% of the CPU each). So I guess it’s just slow to compress the sound in OGG format and SoX couldn’t cope with it. Which makes me wonder how other formats like FLAC compare in terms of speed like that.