Splitting a binary file on binary delimiter? Splitting a binary file on binary delimiter? unix unix

Splitting a binary file on binary delimiter?


You can do this using bbe (http://bbe-.sourceforge.net/) which is a sed like program for binary files:

In order to extract the first JPEG use:

bbe -b '/\xFF\xD8\xFF\xE1/:' -e 'D 2' -o first_jpeg mpo_file

And for the second one:

bbe -b '/\xFF\xD8\xFF\xE1/:' -e 'D 1' -o second_jpeg mpo_file

Note that this will not work if the JPEG's magic number occurs somewhere else in the MPO file.


I think that Bart is on to your biggest problem.. If that binary sequence repeats during the process, you will get partial JPEGs.

I did a quick test by concatenating some JPEGs and then extracting them with awk (please note that the magic number in my files ended in 0xE0 and not 0xE1):

   # for i in *.jpg ; do cat $i ; done > test.mpo    # awk 'BEGIN {RS="\xFF\xD8\xFF\xE0"; FILENUM=-1} {FILENUM++; if (FILENUM == 0) {next}; FILENAME="image0"FILENUM".jpg"; printf "%s",RS$0 > FILENAME;}' test.mpo     # file image0*.jpg    image01.jpg:  JPEG image data, JFIF standard 1.01    image010.jpg: JPEG image data, JFIF standard 1.01    image011.jpg: JPEG image data, JFIF standard 1.01

This seemed to work ok for me, but the above mentioned issues are still unhandled and very real.


I've found a much better explanation of MPO file structure (and how to process it correctly) at http://www.davidglover.org/2010/09/using-the-fuji-finepix-real-3d-w3-camera-on-a-mac-or-linuxunix.html

Edit, October 2019:

Since the blog entry now 404s, here is the script that I wrote based on it. I haven't used it in many years.

#!/usr/bin/env bash# Script to convert 3D MPO files, as used in the Fuji FinePix series of 3D cameras, into standard JPEG files.# Based on work by David Glover, posted at http://www.davidglover.org/2010/09/using-the-fuji-finepix-real-3d-w3-camera-on-a-mac-or-linuxunix.html# This script requires exiftool and ImageMagick.FULLNAME="$1"FILENAME="$(basename $FULLNAME)"DIRNAME="$(dirname $FULLNAME)"BASENAME="${FILENAME%.*}"# Create output directoriesmkdir -p "$DIRNAME"/stereoscopic-rl/mkdir -p "$DIRNAME"/stereoscopic-mpo/mkdir -p "$DIRNAME"/stereoscopic-anaglyph/mkdir -p "$DIRNAME"/monoscopic-l/mkdir -p "$DIRNAME"/monoscopic-r/# Create separate left and right imagesexiftool -trailer:all= "$FULLNAME" -o "$DIRNAME"/monoscopic-l/"$BASENAME"-left.jpgexiftool "$FULLNAME" -mpimage2 -b > "$DIRNAME"/monoscopic-r/"$BASENAME"-right.jpg# Move the MPO file to its new homemv "$FULLNAME" "$DIRNAME"/stereoscopic-mpo/# Determine parallax value and create cropped images for stereo generation# 36 is only appropriate for 4:3 or 3:2 imagesparallax=$(exiftool -b -Parallax "$DIRNAME"/monoscopic-r/"$BASENAME"-right.jpg)parallax=$(echo "$parallax"*36+0.5 | bc | cut -d . -f 1)# The above pipeline can't deal with a parallax of zero# In theory, this fix doesn't cover values between zero and -1# TODO improve the calculationif [ ! $parallax ]; then    parallax=0fiecho $parallaxif [ $parallax -ge 0 ]; then    convert "$DIRNAME"/monoscopic-l/"$BASENAME"-left.jpg -crop +"$parallax"+0 "$DIRNAME"/monoscopic-l/"$BASENAME"-left-cropped.jpg    convert "$DIRNAME"/monoscopic-r/"$BASENAME"-right.jpg -crop -"$parallax"+0 "$DIRNAME"/monoscopic-r/"$BASENAME"-right-cropped.jpgelse    convert "$DIRNAME"/monoscopic-l/"$BASENAME"-left.jpg -crop -"$((-1*$parallax))"+0 "$DIRNAME"/monoscopic-l/"$BASENAME"-left-cropped.jpg    convert "$DIRNAME"/monoscopic-r/"$BASENAME"-right.jpg -crop +"$((-1*$parallax))"+0 "$DIRNAME"/monoscopic-r/"$BASENAME"-right-cropped.jpgfi# Create stereoscopic images for cross-eye (right-left) and anaglyph (red-cyan) viewingconvert "$DIRNAME"/monoscopic-r/"$BASENAME"-right-cropped.jpg "$DIRNAME"/monoscopic-l/"$BASENAME"-left-cropped.jpg +append "$DIRNAME"/stereoscopic-rl/"$BASENAME"-stereoscopic-rl.jpgcomposite -stereo 0 "$DIRNAME"/monoscopic-r/"$BASENAME"-right-cropped.jpg "$DIRNAME"/monoscopic-l/"$BASENAME"-left-cropped.jpg "$DIRNAME"/stereoscopic-anaglyph/"$BASENAME"-stereoscopic-anaglyph.jpg# Clean up separated parallax-corrected imagesrm "$DIRNAME"/monoscopic-l/"$BASENAME"-left-cropped.jpgrm "$DIRNAME"/monoscopic-r/"$BASENAME"-right-cropped.jpgexit 0