Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I played with avconv on Ubuntu and I'm now able to e.g. record the desktop with sound from a soundcard. One thing I wanted to do was recording two video inputs at the same time, for instance the desktop and from the webcam. I thought about doing something like this:

avconv \
  -f alsa \
  -i default \
  -acodec flac \
  -f video4linux2 \
  -r 6 \
  -i /dev/video0 \
  -f x11grab \
  -i :0.0 \
  out.mkv

My thinking was that if you define multiple video inputs, and the .mkv format can handle multiple video streams, avconv will encode 2 video streams and 1 audio stream into one file. But this isn't what happens:

avconv version 0.8.4-6:0.8.4-0ubuntu0.12.10.1, Copyright (c) 2000-2012 the Libav developers
  built on Nov  6 2012 16:51:11 with gcc 4.7.2
[alsa @ 0x1091bc0] capture with some ALSA plugins, especially dsnoop, may hang.
[alsa @ 0x1091bc0] Estimating duration from bitrate, this may be inaccurate
Input #0, alsa, from 'default':
  Duration: N/A, start: 1354364317.020350, bitrate: N/A
    Stream #0.0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
[video4linux2 @ 0x10923e0] Estimating duration from bitrate, this may be inaccurate
Input #1, video4linux2, from '/dev/video0':
  Duration: N/A, start: 100607.724745, bitrate: 29491 kb/s
    Stream #1.0: Video: rawvideo, yuyv422, 640x480, 29491 kb/s, 6 tbr, 1000k tbn, 6 tbc
[x11grab @ 0x107b2a0] device: :0.0+83,87 -> display: :0.0 x: 83 y: 87 width: 854 height: 480
[x11grab @ 0x107b2a0] shared memory extension  found
[x11grab @ 0x107b2a0] Estimating duration from bitrate, this may be inaccurate
Input #2, x11grab, from ':0.0+83,87':
  Duration: N/A, start: 1354364318.488382, bitrate: 196761 kb/s
    Stream #2.0: Video: rawvideo, bgra, 854x480, 196761 kb/s, 15 tbr, 1000k tbn, 15 tbc
Incompatible pixel format 'bgra' for codec 'mpeg4', auto-selecting format 'yuv420p'
[buffer @ 0x107fcc0] w:854 h:480 pixfmt:bgra
[avsink @ 0x10bdf00] auto-inserting filter 'auto-inserted scaler 0' between the filter 'src' and the filter 'out'
[scale @ 0x10dc680] w:854 h:480 fmt:bgra -> w:854 h:480 fmt:yuv420p flags:0x4
Output #0, matroska, to '.../out.mkv':
  Metadata:
    encoder         : Lavf53.21.0
    Stream #0.0: Video: mpeg4, yuv420p, 854x480, q=2-31, 4000 kb/s, 1k tbn, 15 tbc
    Stream #0.1: Audio: libvorbis, 48000 Hz, 2 channels, s16
Stream mapping:
  Stream #2:0 -> #0:0 (rawvideo -> mpeg4)
  Stream #0:0 -> #0:1 (pcm_s16le -> libvorbis)
Press ctrl-c to stop encoding
[mpeg4 @ 0x10bd800] rc buffer underflow
^Cframe=  160 fps= 15 q=2.0 Lsize=    3414kB time=10.66 bitrate=2623.0kbits/s    
video:3273kB audio:131kB global headers:4kB muxing overhead 0.165600%
Received signal 2: terminating.

I'm not sure if it's the question of mapping (some -map options to add?) or that avconv just can't encode more than 1 video stream at one time. So is it an actual avconv limitation, or a limitation of the available containers, or me simply not finding the right combination of command line options?

share|improve this question
    
Try -map 0:0 -map 1:0 -map 2:0 and see what it does. You basically need to map all input streams (input 0,1,2) to the output stream. I can't test it since I have neither avconv nor a Linux system to x11grab from, but it looks as if it only records x11grab and the audio right now. –  slhck Dec 1 '12 at 14:05

1 Answer 1

It's not a good idea to mix input and output options. Hopefully, you'll notice that avconv is converting your audio to Vorbis rather than FLAC - that's because it thinks you're trying to set that as the audio codec of your video4linux device, and is ignoring it as an invalid option. You'd also be best served using x264 as your video encoder. Since you're using Ubuntu, IIRC you should have it as a part of ubuntu-restricted-extras.

avconv -f alsa -i default -f video4linux2 -r 6 -i /dev/video0 -f x11grab -i 0:0 \
-map 1 -map 2 -map 0 \
-c:a flac -c:v libx264 -crf 23 -preset veryfast output.mkv

By default, avconv selects only one video stream and one audio stream (and one subtitle stream if there's one there) to output. -map 1 -map 2 -map 0 tells it to take every stream from the first three inputs (it starts counting from 0).

It's not directly relevant to this (since each of your inputs will contain only one stream), but you can also select individual streams from inputs with -map: -map 0:1 will map the second stream from the first audio, -map 1:a will map every audio stream from the second input, -map 2:v:1 will map the second video stream from the third input and -map 3:s will map all the subtitles from the fourth input.

The order in which you use the -map flags matters; in the example above, I've made it so that the audio input is mapped third, by placing that -map last.

If you have problems with x264 slowing down your screen recording (it shouldn't unless you have a borderline-obsolete computer), you can try changing the -preset to superfast or ultrafast, both of which will give you faster/less-CPU-intensive encoding If that doesn't work, you could try using -c:v huffyuv - that's a lossless video codec, the video equivalent of FLAC, and will give you truly ludicrous file sizes; but you can re-encode to a less painfully big codec later.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.