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.
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:

3 thoughts on “Extracting Video from Motion Photos on Linux”
@Edent I tried to see if iPhone's Live Photos are the same standard. I couldn't work out how to transfer the original file to my computer 🙁
| Reply to original comment on mastodon.me.uk
@Edent I think WordPress may have stripped the other items out of the images when you uploaded it, and it's served up on the blog,
I'm only seeing 964k download size and no embedded video when testing
| Reply to original comment on bluetoot.hardill.me.uk
@ben oooh! You're not wrong!
Looks like image optimisation stuff has been a bit too eager. Will see if I can fix it later.
| Reply to original comment on mastodon.social
More comments on Mastodon.