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


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


Install the script, with the following command

# curl -o /usr/local/bin/
# chmod +x /usr/local/bin/

And extract all the videos in a path, using the following command. /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.

  • Dan Kwon

    Hi Strax,
    I have an un-rooted Galaxy note 8.
    Can I run this script in Android? Should I root my device to run this script?
    Thank you!

    • S74rx

      Hi Dan, you do not need to worry about rooted or unrooted device. You only need a terminal that can run `grep`, `cut`, `tail`.

      I am not sure if you can find a way to run terminal in android. But if you do, you should be able to run the script as you would in normal desktop systems. However, if having a terminal in android requires the device to be rooted, then that’s another story.

      I recommend running the script using a desktop OS, as it is much easier to achieve the result.

      Hope that helps.


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