Record Sound on Ubuntu Docker Image

时间:2017-04-09 23:25:29

标签: ubuntu audio docker ffmpeg

I would like record audio with ffmpeg when I capture my screen. The error I'm getting when using alsa is that is that my image does not have a sound card -f alsa -ac 2 -i hw:0

Here is how to reproduce on a fresh version of Ubuntu

Start a session in a new ubuntu docker image.

docker pull ubuntu
docker run -it --rm ubuntu

Setup alsa (Advanced Linux Sound Architecture)

apt-get update
apt-get install alsa-utils

List the sound cards

aplay -l
# aplay: device_list:268: no soundcards found...

And playing this sound will fail because this image doesn't have any sound cards

sudo aplay /usr/share/sounds/alsa/Front_Center.wav

4 个答案:

答案 0 :(得分:14)

编辑:在阅读有关不需要播放到本地计算机的第一条评论之后

我建议使用pulseaudio,因为它的用户空间代码并不需要内核模块。我觉得alsa不太适合docker(尽管我可能错了)

如果我这样做:

apt-get update
apt-get install pulseaudio socat
apt-get install alsa-utils
apt-get install ffmpeg

# Start the pulseaudio server
pulseaudio -D --exit-idle-time=-1

# Load the virtual sink and set it as default
pacmd load-module module-virtual-sink sink_name=v1
pacmd set-default-sink v1

# set the monitor of v1 sink to be the default source
pacmd set-default-source v1.monitor

# Start the ffmpeg capture to an audio file
ffmpeg -f pulse -i default out.mp3

然后在另一个终端

paplay /usr/share/sounds/alsa/Front_Center.wav

WAV文件的音频由ffmpeg捕获并出现在out.mp3

我真的不知道你的X设置是什么样的,但是如果你可以将音频转到pulseaudio,那么ffmpeg将捕获音频而不需要真正的声卡

原始答案:如果您希望音频转到Mac OS声卡

我最初到了这个页面,因为我试图从VM内部播放音频到Mac OS。但是如果你不关心从虚拟机中获取它,那么以下内容过于复杂。我把它留在这里虽然这个想法是我为什么到这里来的

可以在VM内使用pulseaudio将WAV文件播放到物理声卡。我在Mac OS Sierra上使用docker 17.03.1-ce并使用brew来安装sox。此设置还需要在VM上安装socat,但是具有更多脉冲知识的人(或者如果我有更多时间)应该能够删除此部分我认为

策略是使用paplay通过pulseaudio播放wav文件,并让pulseaudio通过网络在您启动VM时发布的端口上发送音频。在Mac端,您将连接到此已发布的端口,并通过sox将数据发送到Mac。

<强> 1。 MAC:使用已发布端口拉取并运行图像

    docker pull ubuntu
    docker run -it --rm -p 127.0.0.1:3000:3000 ubuntu

<强> 2。 VM:更新并安装require packages

    apt-get update
    apt-get install pulseaudio socat
    apt-get install alsa-utils

我只为wav文件安装alsa-utils,所以你可以删除这个

第3。 VM:启动pulseaudio服务器

    pulseaudio -D --exit-idle-time=-1

这告诉服务器分叉到后台而不是基于不活动退出

<强> 4。 VM:为pulseaudio创建“接收器”

接收器是pulseaudio以特定格式发送音频数据的地方:

    pacmd load-module module-pipe-sink file=/dev/audio format=s16 rate=44100 channels=2

这将以44100Hz的速率将音频发送到带符号的16位双通道文件/ dev / audio。

<强> 5。 VM:将文件附加到网络

此文件现在需要“附加”到网络上,socat用于在发布的地址创建一个侦听套接字(注意:此处没有身份验证),当Mac端​​连接时,该套接字已准备好发送音频

    socat file:/dev/audio tcp-listen:3000

<强> 6。 MAC :从网络读取数据到sox

在Mac端,我们现在需要连接到此端口并通过sox将详细信息发送到音频驱动程序:

    nc 127.0.0.1 3000 | sox -traw -r44100 -b16 -c2 -e signed-integer - -d
默认情况下,mac使用netcat(nc),所以我在这里使用它连接到已发布的端口,然后将数据传递给sox,使用sox标志来匹配上面“load-module”语句中设置的值。

<强> 7。 VM:播放!

最后,使用pulseaudio包中的paplay可以播放WAV文件:

    paplay /usr/share/sounds/alsa/Front_Center.wav

<强> 8。 MAC:听!  在mac端,你现在应该在这里听音频,并能够看到sox的输出,其中包括一个小电平表,这应该移动:

    $ nc 127.0.0.1 3000 | sox -traw -r44100 -b16 -c2 -e signed-integer - -d
    -: (raw)

     File Size: 0
      Encoding: Signed PCM
      Channels: 2 @ 16-bit
    Samplerate: 44100Hz
    Replaygain: off
      Duration: unknown

    In:0.00% 00:00:06.78 [00:00:00.00] Out:299k  [      |      ]        Clip:0

反向操作(即将VM音频发送到VM上的pulseaudio)也应该可以使用类似的,但是向后设置(模块管道源?)

我没有对ffmpeg页面上的命令进行测试,因为我没有X服务器。

答案 1 :(得分:3)

I think the key is on docker run command to map device into the container. Try this:

docker run -ti --rm \
    -v /dev/snd:/dev/snd \
    --lxc-conf='lxc.cgroup.devices.allow = c 116:* rwm' \
    myContainer sh -c "echo run something"`

Extracted from here. On this link there are more options if this doesn't work for you. Check it out!

答案 2 :(得分:2)

This will be much more difficult than you're hoping for. Since Docker runs in an embedded VM, you'd need to first map the sound card to the VM. Once mapped to the VM, you would be able to do a docker run --device=/dev/snd:/dev/snd ... to map the device from the VM into the container.

The issue is the first part, particularly since Mac and Linux are different operating systems. There appear to be some options to install tools that create the cross platform /dev devices needed. Once you do that, you'll need to configure the VM to share these devices into the VM.

答案 3 :(得分:0)

sudo adduser $USER audio

usermod --append --groups audio $USER
这两个命令

都帮助我在linux中显示声卡(具体来说是ubuntu)。在使用docker exec解决问题时,我也在docker容器中遇到了相同的错误,并将这些错误添加到自动化脚本中,并且能够使用arecord -l和aplay -l命令显示声卡。

此外,我使用这些命令来安装必要的alsa和Pulse音频。

sudo apt-get install alsa alsa-utils alsa-tools

sudo apt install pulseaudio pulseaudio-utils