December 6, 2017

How to export video from motion photo in Samsung S7/S8 using a bash script?

Samsung Galaxy S7 and S8 phones have a new way to capture pictures called Motion Photo. When shot in this mode, with each picture, it also records a short video before the picture was captured.

You can view this short video when you view your pictures using the Gallery app of the phone, but when you backup these pictures to your computer, you lose access to them. Below, I will explain a solution which you can use to extract these videos. You basically need a terminal from which you can run grep, cut and tail.

Logic

Logic is quite simple. The video and the photo are stitched on the same file. If you open the picture using a hex editor, towards the end of the file, you will see a portion of binary data that starts with text MotionPhoto_Data which contains the video.

Picture showing MotionPhoto_Data text in a file

Step 1

So, step 1 is to locate the position of this text. You can find this as follows.

grep -abo "MotionPhoto_Data" ./path/to/picture.jpg | cut -d: -f1

The flags specified in the grep mean the following:

  • -a process the binary of the file as text
  • -b prints the byte offset
  • -o prints out matching text

It’s trying to extract the byte offset of the file where MotionPhoto_Data can be found. Then, cut is used to remove other extra bits from the matched text so we can have only the number of offset.

Step 2

Next step is to extract all the data from the file after the position found in step 1. Let’s assume it was 8000. Then, the position we want to extract from is after the text MotionPhoto_Data which is 16 characters long, so from 8017. Then, in our terminal, we can do the following to extract the video

tail -c +8017 ./path/to/picture.jpg > ./path/to/picture.mp4

And, that’s it. Now, you can open your video.

Tool

I have wrapped all this logic into a small script, which can be downloaded and used instantly.

Repository: https://github.com/starx/SamsungMotionPhotoVideoExtractor

Install the script, with the following command

# curl -o /usr/local/bin/extractVideo_SamsungMotionPhoto.sh https://raw.githubusercontent.com/starx/SamsungMotionPhotoVideoExtractor/master/extractVideo_SamsungMotionPhoto.sh
# chmod +x /usr/local/bin/extractVideo_SamsungMotionPhoto.sh

And extract all the videos in a path, using the following command.

extractVideo_SamsungMotionPhoto.sh /path/to/pictures

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

  • Shivz84

    Thanks so much for this. My wife was trying to save motion photos one at a time until I got this working. Very easy to use once I got Ubuntu working on Windows 10 and figured out how to map the SD Card as a drive in Ubuntu.

    • S74rx

      You are welcome. I am glad I was able to help.

...

Please fill the form - I will response as fast as I can!