Terence Eden. He has a beard and is smiling.
Theme Switcher:

Extracting Video from Motion Photos on Linux

· 3 comments · 450 words · Viewed ~396 times


Modern Android cameras can take "Motion Photos". They capture a few seconds of video from before and after you hit the shutter button. You can then either select the bit of the photo where no-one is blinking, or you can send the whole thing as a little movie.

Some apps (like WhatsApp) will play the motion photo when the image is selected, others will just show a static image.

So how do you extract the movie from the image using Linux?

Step one, let's take a look at the EXIF metadata in the image. Here's what running exiftool photo.MP.jpg gets:

Motion Photo                    : 1
Motion Photo Version            : 1
Motion Photo Presentation Timestamp Us: 866808
Directory Item Mime             : image/jpeg, image/jpeg, video/mp4
Directory Item Semantic         : Primary, GainMap, MotionPhoto
Directory Item Length           : 46353, 2106347
Directory Item Padding          : 0
MPF Version                     : 0100
Number Of Images                : 2
MP Image Flags                  : (none)
MP Image Format                 : JPEG
MP Image Type                   : Undefined
MP Image Length                 : 46353
MP Image Start                  : 2570425

That can be cross-referenced with the Motion Photo metadata specification.

We can confirm this is a Motion Photo, Version 1. The video portion at 866,808 microseconds (about 0.8 seconds) is where the main photo is taken from.

The file starts with the image, then a GainMap (for HDR), and then the video.

Somewhat obtusely (in my opinion) the Directory Item Length only shows "secondary media items" - in this case, the GainMap and Video.

The filesize is 4,723,125 bytes, which equals the sum of the three values; 46,353 + 2,106,347 + 2,570,425.

So, to get the MP4 video, we need to extract the last 2,106,347 bytes. This can be double-checked by taking the filesize and subtracting the MP Image Start and the MP Image Lengths (4,723,125 - 46,353 - 2,570,425 = 2,106,347).

The extraction can be done with dd but it's probably just as easy to use tail to read the last N bytes of a file:

tail -c 2106347 photo.MP.jpg > video.mp4

You can verify that the video is valid by running ffmpeg -i video.mp4 - the output will be lower resolution than the photo and will only be a few seconds long. It will play in VLC or any other standard player.

Try It Yourself

Here's one of my motion photos - it should present in your browser as a still image, but run the above code to extract the video.

Photo of a wind turbine.

Click the photo to download the full version rather than the optimised one.

Sources

For other adventures in Motion Photo exploration, take a look at:


Share this post on…

3 thoughts on “Extracting Video from Motion Photos on Linux”

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

See allowed HTML elements: <a href="" title="">
<abbr title="">
<acronym title="">
<b>
<blockquote cite="">
<br>
<cite>
<code>
<del datetime="">
<em>
<i>
<img src="" alt="" title="" srcset="">
<p>
<pre>
<q cite="">
<s>
<strike>
<strong>

To respond on your own website, write a post which contains a link to this post - then enter the URl of your page here. Learn more about WebMentions.