Getting Sound and Video to Work on Raspberry Pi

Getting Sound and Video to Work on the Raspberry Pi

NOTE: There is a new default operating system for the Raspberry Pi that includes omxplayer. The following post, assumes that you are using the original Debian image for the SD card image of your operating system. To learn more, please see my post Raspbian Optimize Raspberry Pi.

After following Slicing into the Raspberry Pi, your Debian system should be booting and updated. In order to have room for the audio and video software, you should also followed Getting a bigger slice of Raspberry Pi to expand your root file system.

This post describes how to get audio and small videos to play back within Debian on the Raspberry Pi.
If you want sound and video to work without having to make the following changes, and take advantage of hardware playback, then you might want to set up OpenElec as described in OpenElec on the Raspberry Pi.

Update: If you want to get video to high definition video to play back under Debian Linux, then you may want to look into omxplayer compiled to take advantage of the GPU on the Raspberry Pi, this is described in my new post Playing HD Videos in Debian on the Raspberry Pi.

Playing the right videos

If you want to be able to play videos using Debian on the Raspberry Pi, then they often have to be scaled down to be able to play without hardware acceleration, and I've already mentioned how to get a distribution that does take advantage of hardware to accelerate play back (OpenElec). What I use to transform videos to use different codecs and target sizes is ffmpeg.

As an example, there was no way to play back a full 720p video encoded with ogg/theora without it either freezing the system, or having horrible play back 

Thus, I transformed the Big Buck Bunny video to be able to play by using the following. The original file was named big_buck_bunny_720p_stereo.ogg. It's video is encoded with the mpeg4 video codec at a bit rate of 22050 bits per second. The audio is encoded as mp3 with a frequency of 44100 Hz. The target is what gives it a size of 352x240 pixels. The new name of the encoded file is big_buck_bunny.mp4.

ffmpeg -i big_buck_bunny_720p_stereo.ogg  -vcodec mpeg4 -ar 22050 -acodec libmp3lame -ab 44100 -target ntsc-vcd big_buck_bunny.mp4

Updating the Firmware

Without updating the firmware, the Raspberry Pi will probably freeze it you attempt to play media.
It did to me before updating the firmware. Afterwards, sound and video play back worked.

After updating the firmware, the system will also run smoother with the newer kernel. While it can be done manually, it is much easier to use a script created by Hexxah to update the firmware of your Raspberry Pi. The README.md file for this script also explains advanced usage which can allow you to split the memory between the system and graphics:

  • Install software needed to perform the update:
    • pi@raspberrypi:~$ sudo apt-get install ca-certificates git-core binutils
  • Download the script:
    • pi@raspberrypi:~$ sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update
  • Copy the script to /usr/local/bin:
    • pi@raspberrypi:~$ sudo cp rpi-update /usr/local/bin/rpi-update
  • Make the script executable:
    • pi@raspberrypi:~$ sudo chmod +x /usr/local/bin/rpi-update
  • Run the script:
    • pi@raspberrypi:~$ sudo rpi-update
    • In the future to check for updates, just execute this last step!
The output of the script should like the following, including the error about no such file or directory:

Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS
Performing self-update
Autodetecting memory split
Using ARM/GPU memory split of 192MB/64MB
We're running for the first time
Setting up firmware (this will take a few minutes)
Using SoftFP libraries
/opt/vc/sbin/vcfiled: error while loading shared libraries: libvchiq_arm.so: cannot open shared object file: No such file or directory
If no errors appeared, your firmware was successfully setup
A reboot is needed to activate the new firmware

Enabling the Sound Module

In order to enable sound to work, add a line to /etc/modules to load the sound module automatically at boot time:
  • pi@raspberrypi:~$ sudo su -
  • root@raspberrypi:~# echo 'snd-bcm2835' >> /etc/modules
  • root@raspberrypi:~# exit
To view the modules loaded use:
  • pi@raspberrypi:~$ lsmod
Before loading the sound module, the output should look like:
Module                  Size  Used by
fuse                   49036  1 
evdev                   6404  2 

After either rebooting, or using sudo modprobe snd-bcm2835 to load the module immediately, the output of lsmod should look like this:
Module                  Size  Used by
snd_bcm2835            15388  0 
snd_pcm                49240  1 snd_bcm2835
snd_timer              13560  1 snd_pcm
snd                    31624  3 snd_bcm2835,snd_pcm,snd_timer
snd_page_alloc          2660  1 snd_pcm
fuse                   49036  1 
evdev                   6404  2 

Installing Sound and Video Software

For video media players, I tried VLC, and Totem, but it either wasn't working for me in the case of VLC, or it didn't work smoothly Totem. What I found worked was Mplayer

The system also requires a sound server and sound driver, which I found installing pulseaudio and alsa-base provided. For testing sound with wave files, I also installed alsa-utils, which provides aplay, to play a sound file.

  • Install the software:
    • pi@raspberrypi:~$ sudo apt-get install mplayer mplayer-gui alsa-base alsa-utils pulseaudio mpg123
  • Reboot the system
    • pi@raspberrypi:~$ sudo reboot

Command Line Audio Playback

If you want to test whether sound is working quickly, then the alsa-utils includes aplay, which can play back wav files, but not mp3 files. If you want to be able to play back mp3 files and more, then install mpg123. For very sophisticated control, there is also the xmms2 client, which has many plugins. Given that there are several GUI clients for xmms2, including a default installation of lxmusic,  it may be easier to control it with visual controls.

pi@raspberrypi: ~ $ mpg123 /usr/share/scratch/Media/Sounds/Vocals/Come-and-play.mp3
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
version 1.14.2; written and copyright by Michael Hipp and others
free software (LGPL/GPL) without any warranty but with best wishes
Playing MPEG stream 1 of 1: ilovelinux.mp3 ...

MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo

Command Line Video Playback

I was frustrated at first trying to get video to playback from the command line. If I ran mplayer as an ordinary (pi) user, then it played back sound, but no video appeared. I ran sudo mplayer, then I could see video, but I could not hear sound. I discovered that device /dev/fb0, which is the framebuffer video device is owned by the "video" group. To solve this problem, the pi user must be added to the video group to be able to write to the framebuffer device.
  • Modify the pi user to belong to the video group:
    • pi@raspberrypi:~$ sudo usermod -aG video pi
  • Logout and login for changes to take effect:
    • pi@raspberrypi:~$ exit
If you expect large videos to play smoothly, then again I refer you to OpenElec. I found some videos that I converted to play on my old GPS  unit that were only 360x240 resolution, and they were able to play smoothly by adding a few options to mplayer as I started it up:

Playing back a video can be as simple as typing: mplayer /path/to/video.avi. However, your video may not play back, and the system may even have to be rebooted to be unfrozen. 

Rather than using many options on the command line, I use the /home/pi/.mplayer/config file to customize playback settings for mplayer, so that it optimizes the play back for the slow CPU, and audio to the correct frequency.  Only the lines with the bold entries need to be added to the file /home/pi/.mplayer/config:

# Let sdl decide video output which will use the /dev/fb0 device or X if it is running

# Decoding options for the video: lowres, fast, and skipfilter
# lowres=1 is half size
# lowres=2 is quarter size
# lowres=3 is eighth size 
# Use higher values of lowres if unable to play back smoothly
# fast optimizes MPEG-2, MPEG-4, and H.264 videos 
# skiploopfilter=none produces the best quality
# Other skipfilter settings include all and nonref which look worse but play back smoother
lavdopts=lowres=3:fast=1:skiploopfilter=all # most aggressive

# vfm can be a comma separated list of video family codecs to use

# ao is the audio output and should be pulse if using pulseaudio

# af is the audio filter, using re-sampling in this case, to provide audible sound

# Allow framedropping might reduce video quality, but allows video and audio to stay synced
framedrop = yes

GUI Play Back


Surprisingly, the video play back seems to work better in the GUI! Out of all the alternatives, mplayer or mplayer-gui seem to work best, or at all for me. VLC failed to work for me, and Totem had huge Gnome requirements and slow play back.

If you installed mplayer-gui package, then in the menu under Sound and Video, you will find the application Mplayer to start mplayer-gui. If you open a terminal, then you can also launch mplayer /path/to/video.mpg to view your video like:

pi@raspberrypi:~$ mplayer /usr/share/pyshared/pygame/examples/data/blue.mpg


Out of the box, the lxmusic application is installed. The easiest way to start it is from the menu under Sound and Video as the Music Player application. There a couple of other xmms2 based players available like abraca and promoe. Don't forget that mplayer can also be used as a audio player.

It is not hard to find lists of music players for Linux, but finding one that doesn't use to much RAM or require too much space is a concern for the Raspberry Pi. I also tried to use audacious, but there appears to be a serious problem with it combined with mpg123 or mpg321, where it is unable to play sound.


Unknown said...

Latest firmware, latest raspbian, video is fine from command line but when it comes up to gmplayer, I get a message: video not found!

Anonymous said...

Thanks dude, it works.
convert from mp4 to avi mac osx

AdriZ said...


Check this tutorial on how to enable sound on raspberry pi.


Michael Gade said...

ffmpeg is very useful for computer geeks. As a computer novice, I prefer to use video converter assist program and it's extremely easy to use.

Mark Newton said...

Great article! ffmpeg is nice tool for transforming video to play back on Raspberry Pi. I prefer to use the Video Converter Assist program, it's easier.