FFMPEG is one of those tools I use when I just want to quickly hack together a video and don’t need fancy things like editing, titles, or a user-interface. Compiling on a regular computer isn’t easy, but compiling for the Raspberry Pi takes a little more patience and care. I also wanted to include support for H264 video, which needs to be installed before compiling FFMPEG.
There are lots of examples on the web, but what worked for me was a combination of a few of them, so here’s what I did. Note many of these commands may require sudo appended before (commands like git , ./configure, and make )
- INSTALL H264 SUPPORT
Run the following commands, one at a time.
123456cd /usr/srcgit clone git://git.videolan.org/x264cd x264./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-openclmakesudo make install - INSTALL OTHER LIBRARIES/FORMATS
Anything else you would like to install should be done now, before compiling FFMPEG. This includes MP3, AAC, etc. - INSTALL FFMPEG
Add lines similar to the --enable-libx264 for anything else installed above. This may take a REALLY long time, so be patient.
123456cd /usr/srcgit clone https://github.com/FFmpeg/FFmpeg.gitcd ffmpegsudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfreemakesudo make install
As Malcshour notes, if you have a Model B+ you can use make -j4 instead of just make to take advantage of all four cores!DONE! TEST IT
To test your new install, simply run the command ffmpeg . If you don’t get any errors, you’re all good. What did I do with my new tool? I built a bot that generates random guitar chords.
A FEW MORE RESOURCES
Thanks for the guide.
Can you help me enabling mp3 support?
It should be pretty easy. This guide says to use the following commands (one-by-one in the Terminal):
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.tar.gz
tar xzvf lame-3.99.tar.gz
cd lame-3.99
./configure
make
make install
For anyone getting
“Cloning into ‘ffmpeg’…
fatal: The remote end hung up unexpectedly”
Try
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
instead
Could you not supply prebuild versions?
Thank you for a simple walkthrough!
Took more than 3 hours to compile ffmpeg on a Raspberry PI Model A+, i went to sleep after so i don’t know the exact time it took, but that VERY long process for this machine. And I’m only talking about «sudo make» for ffmpeg… Installing h264 took me almost 2 hours too…
So don’t expect to do this quickly if you don’t have a day to spend on this.
Also, like Alex said, the ffmpeg source git repo is now:
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
instead of:
git clone git://git.ffmpeg.org/ffmpeg
Many thanks Jeff, for the tutorial.
I’m getting “illegal hardware instruction” error when trying to use ffmpeg. Also same error occurs if I run compiled x264 binary.
Hmm, not sure what’s causing that. That means you’re trying to compile for a device with different hardware than the Pi. Are you definitely including the
--arch=armel
command? Unfortunately, this could also be an issue in the newest version of FFMPEG as well.Thank you it worked for me after a long time of researches on the web!!
PS: If someone has this error:
“ar: libavcodec/libavcodec.a: No space left on device
library.mak:40: recipe for target ‘libavcodec/libavcodec.a’ failed
make: *** [libavcodec/libavcodec.a] Error 1”
Don’t worry to run “sudo raspi-config” and select the first option “expend Filesystem”. this will use the whole SD card size. Then reboot your RPI and run once again the command: sudo make, etc. (sorry for my English, I’m french)
Followed the steps to the letter and things seemed to work without error messages. However, when I type ffmpeg at the end of it all, I get the message
-bash: ffmpeg: command not found
Any ideas??
Hmm. Have you tried rebooting the Pi and then trying it again? It might take a restart to show up. Otherwise, I might just try installing again and see what happens. Error messages on the Pi don’t always appear, even when there is an error.
Sorry, when i use “make”, i have this error…
libavdevice/lavfi.c: In function \u2018lavfi_read_packet\u2019:
libavdevice/lavfi.c:433:9: warning: \u2018avpicture_get_size\u2019 is deprecated (declared at ./libavcodec/avcodec.h:4828) [-Wdeprecated-declarations]
size = avpicture_get_size(frame->format, frame->width, frame->height);
^
libavdevice/lavfi.c:437:9: warning: \u2018data\u2019 is deprecated (declared at ./libavcodec/avcodec.h:3614) [-Wdeprecated-declarations]
memcpy(pict.data, frame->data, 4*sizeof(frame->data[0]));
^
libavdevice/lavfi.c:438:9: warning: \u2018linesize\u2019 is deprecated (declared at ./libavcodec/avcodec.h:3616) [-Wdeprecated-declarations]
memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0]));
^
libavdevice/lavfi.c:440:9: warning: \u2018avpicture_layout\u2019 is deprecated (declared at ./libavcodec/avcodec.h:4820) [-Wdeprecated-declarations]
avpicture_layout(&pict, frame->format, frame->width, frame->height,
Every thing went well without errors, but my system cant find the packages how o add it?
Just a quick tip:
If you have a Model B+ (4-Cores) you can do “make -j4” instead of just “make” to make it use all four cores instead of just one.
Speeds up the process quite a bit!
Thanks for that great article!
This is great – tutorial updated!
I guess I will that since my Pi always has some ressources to spare.
Also, it’s the Pi 2 B that has 4 cores, not the B+.
Can I play a preview of the video after ffmpeg finishes?
Using R-Pi Model B
It does a great job, but when recording finishes, I put omxplayer av.mkv command after the ffmpeg command (using python), but omxplayer plays the av.mkv file form my previous attempt. It looks like omxplayer may be starting before ffmpeg finishes saving the new av.mkv file.
Any suggestions? (I am fairly new to Pi and python, so have mercy!)
Are you trying to do this as one script (ie write to ffmpeg and then play)? If so, omxplayer is probably starting to run before ffmpeg finishes – since ffmpeg writes to streams, they can be played even when they’re not done. I would look at the Python
subprocess
module (which might be what you’re using) and specifically Popen’scommunicate()
method.Just to confirm, the command line argument I used for the Pi 2 to use 4 cores was: sudo make -j4 install
you said the b+ has 4 cores it does not the pi 2 and pi 3 does and thats where you would use -j 4 command on make and make install
git clone git://git.videolan.org/x264
fatal: could not create work tree dir ‘xx’.: Permission denied
What do I do to this?
You’re probably trying to clone into a directory you don’t have permission to write to. You should either use the
cd
command to move into a different directory, or use thesudo
command and enter your password to get access (ie:sudo git clone git://git.videolan.org/x264
.Running the ffmpeg configure script (after installing libx264) I’m getting ‘ERROR: libx264 not found’. Anyone else getting this? I’m sure there’s something I’m doing wrong here.
Thanks for the excellent tutorial. Does ffplay use the RPI 3’s GPU acceleration for playing rtsp streams? Trying to get over the omxplayer latency issue.
Hmm, this is an error from installing H264 support. You could try running ffmpeg with a different codec and see if that works to confirm. I would suggest uninstalling everything and starting again.
Hardware Encoding is now supported!!!
1. update to latest version: “git pull”
2. add “–enable-omx –enable-omx-rpi” to ./configure options
3. recompile
4. use encoder option “-c:v h264_omx”
References:
http://ubuntu-mate.community/t/hardware-h264-video-encoding-with-libav-openmax-il/4997
http://ffmpeg.org/pipermail/ffmpeg-devel/2016-May/193895.html
Before I try:
What OS are you using?
Do you manage to play ffmpeg in HD?
Thanky for this great post!
Thanks teknoid, will have to try that!
I use Raspian, but haven’t tried HD. I’d say try it and if it doesn’t work, you can always delete ffmpeg and the other files.
Hi Michael,
I’m encoding – not playing ;-) I use stock Raspbian jessie with latest rpi-update:
hje@picam:~$ cat /etc/issue
Raspbian GNU/Linux 8 \n \l
hje@picam:~$ uname -a
Linux picam 4.4.8-v7+ #880 SMP Fri Apr 22 21:55:04 BST 2016 armv7l GNU/Linux
hje@picam:~$
Compilation was easy, the only thing I changed was commenting out a line in libavcodec/utils.c (line 1956). Return code seems to be codec->encode2(avctx, avpkt, frame, got_packet_ptr);
// av_assert0(ret <= 0);
I'm using it to create daily timelapse videos from my webcam. The webcam creates 1280×720 images every 10 seconds via uvccapture. In the past I used gstreamer for encoding but these videos couldn't be played over html5 embedded tag. IMHO gstreamer maybe ok for a professional AV system – but not for a tiny embedded system such like Raspberry. So I’m very happy now that ffmpeg can do the job and I don’t need this stupidly overloaded gstreamer shit anymore…
Thanks for the guide!
Hi,
I’ve used Technoid’s valuable instructions to rebuild my custom ffmpeg, incorporating hardware h264 encoding.
After commenting out line 1956 in libavcodec/utils.c [ av_assert0(ret <= 0); ], I'm able to run ffmpeg with the new encoder.
Unfortunately the results when transcoding video are somewhat underwhelming. The resultant file is massively compressed, and always 1MB regardless of which switches I pass (constant rate factor, scaling, audio codec etc)
A discussion on github suggests that it's very early days for the hardware h264 encoder, but I wondered if anyone else has had better results?
https://github.com/ccrisan/motionpie/issues/250
For the record, encoding a 30 second audio video file of an Atari ST demo screen:
Hardware: Raspberry Pi 3, overclocked to 1350mhz
Encoder: omx_h264
Resultant file: very poor framerate, blurry
File size: 1MB
Encoding speed – solid 10 fps, c. 22% cpu (total across all cores)
Command:
/home/pi/bin/ffmpeg -i hatari_bmp-atg-megademo_stretch-scroller.avi -c:v h264_omx -crf 1 -r 25 -pix_fmt yuv420p -vf scale=-1:ih*2 -c:a aac output-hard264.mp4
Encoder: libx264
Resultant file: video and audio quality are flawless
File size: 38MB
Encoding speed: 7 -10 fps, c 93% cpu across all cores
command:
/home/pi/bin/ffmpeg -i hatari_bmp-atg-megademo_stretch-scroller.avi -c:v libx264 -crf 1 -r 25 -pix_fmt yuv420p -vf scale=-1:ih*2 -c:a aac output-standard_h264.mp4
Thanks Jeff & teknoid!
The installation described worked without any problems on a Pi3 with Raspbian Jessie (via Noobs v1.9.1 form 10. May 2016).
But actually I misunderstood the whole topic, as ffmpeg here strictly means the video conversion tool and not the codec for video playing as I had thought!
My goal is to make one of the seamless video loopers play ProRes/FFmpeg content in something like 1920×1200 px. My content has very clean gradients which easily get messed up by the common compression methods. I´ll keep fiddling around…
Or does anybody here have advise for
– playback of ffmpeg
– best seamless video looper
(both for RaspberryPi 3) ?
The FFmpeg source repo is now available at:
https://github.com/FFmpeg/FFmpeg.git
Use following command to get the FFmpeg source:
git clone https://github.com/FFmpeg/FFmpeg.git
Thanks!
Amit
Updated, thanks!
I have a RPI 3, followed the instructions, and used -j4 for both make commands. When I typed ffmpeg, couldn’t find the command. but it did work after a reboot. Thanks for this great article.
Yes, a reboot often is required (and is a good thing to try if a command seems to have failed to install). Glad this helped!
Oh man. This is taking soooo long. Any idea how long it takes though?
@Keerthana: It can take several hours or even overnight.
Thanks for this guide. I’m also setting up my RPi B+ as a time-lapse camera. I’ve got a cron job taking a photo of my tortoise enclosure every 1min, and plan to use ffmpeg to convert this to a video file and upload it to YouTube every night.
The first proof of concept is here:
https://www.youtube.com/channel/UCqhWDL1E_yo9RQgA1ibDo1g
For this first video I used ffmpeg on my laptop to create the video. FFMpeg is compiling on my RPi B+ as I type this. A failed attempt died after 3.5hrs. (I’ve now commented out the assert on line 1956 as TekNoid suggested, and I’m giving it another try).
If anyone’s interested, here are the ffmpeg options that worked on my laptop (ffmpeg 3.1.1), which I plan to use on the RPi:
> ffmpeg -r 15 -pattern_type glob -i ‘images/*.jpg’ -c:v libx264 timelapse.mp4
I should also note that the latest version of ffmpeg (as of July 2016) supports hardware encoding (awesome!) however the
make
command does NOT!If you attempt to compile ffmpeg using
make -j 4
as suggested above, the compile will not complete. (Learned that the hard way!!) :)Note that you need to have the libSDL libraries in order for ffplay to compile.
Without libSDL, configure doesn’t complain at all; and configure –help only mentions an option to *disable* SDL. So it’s easy to simply assume that since it’s not complaining about SDL, you shouldn’t have a problem.
However the result after hours of compilation (seriously though, this stuff is better cross-compiled on a proper desktop) is that there’s no binaries for ffplay, as I’ve learned the hard way. So if you’re interested in ffplay, make sure you have the libSDL libraries.
Hi Jeff,
Thanks for the great guide.
If I run this command:
sudo ffmpeg -f alsa -i hw:1 -t 30 out.wav
to record from my soundcard, ffmpeg returns an error saying format ‘alsa’ not recognized. like this : Unknown input format: ‘alsa’
I need the alsa libraries to record audio, what to do?
Thanks!
@Suraj – I’ve not used alsa before. Do you have it correctly installed? If not, maybe this guide can help? I also found this info on using alsa with ffmpeg, which might be helpful too.
Hi Jeff,
Thanks for such a prompt reply!
Actually I was doing a raspberry pi live streaming to youtube project and I needed the sound from my soundcard as well to stream to youtube with the video. Video was streaming nicely earlier. BUT problem solved! when I compiled from this source: git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
This ffmpeg version does have alsa libraries!
I issued the command above :
sudo ffmpeg -f alsa -i hw:1 -t 30 out.wav
And now it recognizes the alsa format!
Thank you so much!
OMG I tried so many tutorials and only this worked perfectly on my Raspberry Pi 3 Model B thank you!!
I transcoded and streamed an MPEG4 file from my RPi on a udp address, I haven’t test the sound yet though.