This is going to be short and sweet. I picked it up from a video by the Marvellous Matthew Manning (reminds me of the “marvellous mechanical mouse organ” from Bagpuss – Google it if you need to ask) otherwise known as RaspberryPi4Beginners.
Matt’s made a video which complements several of my recent blogs about the Raspberry Pi Camera. His channel is Raspberry Pi For Beginners, but that doesn’t mean that only beginners can learn something from it. I learnt this alternative way of wrapping an .h264 stream to .mp4. It’s much simpler than using FFMPEG and it worked first time for me. (Apparently Stuart Green @sg_84 figured it out).
Installation
sudo apt-get update
sudo apt-get install gpac
y
Usage
MP4Box -add filename.h264 filename.mp4
And you can do it all right there on your Raspberry Pi. Nice one Matt. Check out the full video here…
I met Matt at the Cambridge Jam last week. We had a bit of a chat, but not enough. I find Jams a bit hectic (particularly when I have three demos) and although good for initial meet-ups, perhaps not the best places for lengthy discussion. We must meet again soon and have a proper chat!
Nice one! Been hinting ages for this!
The Raspberry Pi Guy
Didn’t you see this one I published a week ago? https://raspi.tv/2013/how-to-shoot-video-and-convert-it-to-something-you-can-edit-in-pinnacle-and-other-programs
Ah, missed that! With ‘gpac’… How does it actually work? One of my friends said something about MP4s being containers? Je ne sais pas!
It takes the stream and puts it in an MP4 container or wrapper – just like your friend said. Seems to be needed for video editing programs to handle it. A ja, nie wiem duzo wiecej.
Ah, Thanks! My editing program wasn’t reading it at all (Corel x5)… Let me guess… Was that a tad of Polish? French and Spanish are my limits!
Yes
The MP4Box is running perfectly on raspberry, but I have one doubt whe I read AVC-H264 import – frame size 1920 x 1080 at 25.000 FPS. Is the vid 25 FPS? or perhaps 30 FPS
The output is 25 fps – at least, that’s what it says on the MP4 file when I look in Windows. I don’t know if there’s a way to get it in 30 fps or not. I expect there is, but I don’t know how to do that. I’m happy with 25 fps as it avoids issues for me when mixing video from different sources. My main video camera is 25 fps.
How can I do this command in a Python script?
You can pretty much do anything in Python if you
from subprocess import call
near the top of your program, then…
your_command = "do this" # set your command here
call ([your_command], shell=True)
[…] And this is possible on the raspberry pi itself. […]
I was wondering if there’s a way to stream h.264 video from a USB camera / Raspberry pi to a windows computer. It has to be a way other than gstreamer as I’ve heard it’s quite tricky to stream with this software on a windows machine.
Hi!
Do you think it’s possible to write something like this:
from subprocess import call
converter = MP4Box -add filename.h264 filename.mp4
Call = ([converter], Shell = True)
?
Yes it is but you’d need quotes around at least the string parts of the command…
converter = “MP4Box -add filename.h264 filename.mp4”
After sudo apt-get install gpac. Not all files can be authenticated but I download anyways. A lot of errors about things I cannot get. Raspberry pi says MP4Box is not a valid command. How do I convert the .h264 now? I’ve sudo apt-get upgrade and updated already. I don’t know what to do.
After MUCH trial and error here’s what I got to work. I used Popen in order to call the .wait() function later to get a return code for debugging.
import subprocess
convCommand = ‘MP4Box -add {0}.h264 {1}.mp4’.format(filename, filename)
conv = subprocess.Popen(convCommand, shell = True)
Could you show me whole code?
import subprocess
convCommand = ‘MP4Box -add {0}.h264 {1}.mp4’.format(filename, filename)
conv = subprocess.Popen(convCommand, shell = True)
I’ve got NameError: name ‘filename’ is not defined
I’ve got video.h264 at this name.
convCommand need to be a list, not a string. Popen doesn’t read strings.
convCommand = ‘MP4Box -add {0}.h264 {1}.mp4’.format(filename, filename)
Should be:
convCommand = [‘MP4Box’, ‘-add’, FILEIN + ‘.h264’, ‘-o’, FILEOUT + ‘.mp4’]
It depends whether you use the shell=True flag or not.
https://docs.python.org/2/library/subprocess.html
Thanks, works like a charm
trying sudo apt-get install gpac (or -y gpac) wouldnt work for me, saying it is unable to locate the package. Is there anything in particular I need to do?