Extracting Video from Motion Photos on Linux
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.
## Sources
For other adventures in Motion Photo exploration, take a look at:
* https://developer.android.com/media/platform/motion-photo-format
* https://motion-live.js.org/
* https://medium.com/android-news/working-with-motion-photos-da0aa49b50c
Extracting Video from Motion Photos on Linux 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 t...
#/etc/ #exif #linux #photos
Origin | Interest | Match