shinagan
Posts: 9
Joined: Fri Oct 10, 2014 3:19 pm

Convert MJPEG output to mp4

Sat Jun 15, 2019 6:19 pm

Hello,

I have a few videos taken at maximum resolution with the camera v2 (3280x2464) that are in MJPEG format due to the h264 limitation to ~ full HD.
What command line tool can I use to convert these MJPEG streams to mp4 format?

Thanks!

User avatar
HermannSW
Posts: 1239
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Convert MJPEG output to mp4

Sat Jun 15, 2019 9:27 pm

ffmpeg:
https://www.google.com/search?&q=conver ... mmand+line

P.S:
Off topic, I just used ffmpeg to cut out part of twitter video, and gifenc.sh to convert to animated .gif (robot's response is nice):
https://twitter.com/HermannSW/status/11 ... 0042497024
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

shinagan
Posts: 9
Joined: Fri Oct 10, 2014 3:19 pm

Re: Convert MJPEG output to mp4

Sat Jun 15, 2019 10:03 pm

Well I've looked at a few of these answers and nothing has seemed to work so far.
I thought ffmpeg would be the way to go and tried the following:

Code: Select all

ffmpeg -i input -c:v libx264 -preset veryslow -crf 18 output.mp4
I get that preset is not known. When I remove it, ffmpeg says

Code: Select all

Unknown encoder 'libx264'
I have installed libavcodec-extra57 -> it did not solve the problem. Still unknown.
I'm not sure what ffmpeg options are required to achieve this, if you have had it working would you mind sharing what options you use? Thanks!

User avatar
HermannSW
Posts: 1239
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Convert MJPEG output to mp4

Mon Jun 17, 2019 3:49 pm

shinagan wrote:
Sat Jun 15, 2019 10:03 pm
I'm not sure what ffmpeg options are required to achieve this, if you have had it working would you mind sharing what options you use?
I tried several of the mjpeg2mp4 solutions and none worked.

Then I went back to gstreamer, and was able to readout and display mjpeg stream from ESP32-CAM module CamWebserver example:

Code: Select all

$ gst-launch souphttpsrc location=http://192.168.178.157:81/stream timeout=5 ! jpegdec ! autovideosink
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock

So viewing the mjpeg stream is no problem. I know that mp4mux can be used to create .mp4 video, but was not able to get the jpeg decoded stream of frames stored as .mp4, but reading&decoding is done, so you can start from there.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6895
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Convert MJPEG output to mp4

Mon Jun 17, 2019 4:10 pm

You've said mp4, which is a container format and not a codec. mp4 can hold a huge number of codec formats.

You can put mjpeg into an mp4 container with

Code: Select all

ffmpeg -r 15 -i foo.mjpg -c:v copy bar.mp4
Transcode is a much bigger job, and will be very slow at 8MPix resolution on the CPUs.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
HermannSW
Posts: 1239
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Convert MJPEG output to mp4

Tue Jun 18, 2019 7:31 am

6by9 wrote:
Mon Jun 17, 2019 4:10 pm
You can put mjpeg into an mp4 container with

Code: Select all

ffmpeg -r 15 -i foo.mjpg -c:v copy bar.mp4
Thanks, this works with http mjpeg stream as well (I used wget, maybe there is a direct ffmpeg way to do so as well):

Code: Select all

$ ffmpeg -r 15 -i <(wget http://192.168.178.157:81/stream -O -) -c:v copy bar.mp4
Just terminate the recording+conversion with CTRL-C. The .mp4 plays way too fast in totem video player because of missing time stamps.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

Return to “Camera board”