Batch conversion for RICOH THETA S 360 degree movies

# tl;dr;
You can convert a RICOH THETA S movie with the following command:

```sh:
/Applications/RICOH\ THETA.app/Contents/Resources/tools/DualfishBlender.osx \
  source_file.MP4 \
  output_file.MP4
```

# Intro
If you took a 360-degree movie by using [RICOH THETA](https://theta360.com/en/), you need to convert the raw *.MP4 file into a stitched *.MP4 file. On Mac OS X, [RICOH THETA for Mac](https://theta360.com/en/support/download/pcmac/) application can be used for the conversion. But it doesn't support batch conversion, you need to drag-and-drop the raw *.MP4 files one-by-one manually. It's really annoying, especially if you have a handful number of movies.

This post explains how you can convert multiple movies at once. The following configurations are assumed:

 * Device: RICOH THETA S
 * OS: Mac OS X v10.10
 * Tool: RICOH THETA for Mac v2.1.1

# Download movies from RICOH Theta S to your Mac OS X
You can use the Image Capture to download movies from RICOH Theta S. It is one of preinstalled tools on Mac OS X. Since it is out of the scope of this post, I won't explain it in detail. Please check it by yourself :)

# CUI conversion
The RICOH THETA for Mac provides a GUI, but it also contains a command line tool which can be invoked on the terminal directly. You can find the tool at the following location by default. If you installed RICOH THETA for Mac in any other location, please adjust the file path accordingly:

`/Applications/RICOH THETA.app/Contents/Resources/tools/DualfishBlender.osx`

This tool takes 2 arguments, `source` and `output`. If you have `R0010001.MP4`, and would like to convert it into `R0010001_er.MP4` (default naming convention by the GUI), you can use the following command:

```sh:
/Applications/RICOH\ THETA.app/Contents/Resources/tools/DualfishBlender.osx R0010001.MP4 R0010001_er.MP4
```

# Batch conversion
The command line tool lets you convert the movies without GUI, but it still needs manual invocation on each *.MP4 files. The following script searches all *.MP4 files in the current directory and its subdirectories recursively, and stitch them automatically.

```sh:
#!/bin/sh

DUALFISH_BLENDER=/Applications/RICOH\ THETA.app/Contents/Resources/tools/DualfishBlender.osx
SUFFIX=_er
if [ "${1}" = "dry" ]; then
  DRY=echo
fi

find . -name '*.MP4' \
  | sed "s/\.MP4$//; s/${SUFFIX}$//;" \
  | sort \
  | uniq -c \
  | grep '^  *1 ' \
  | awk '{print $2}' \
  | xargs -n 1 -I % ${DRY} "${DUALFISH_BLENDER}" %.MP4 %${SUFFIX}.MP4
```


# For RICOH THETA m15 users
The RICOH THETA m15, which is the previous version of RICOH THETA S, outputs a raw movie as a *.MOV file. The command line tool (v2.1.1) also supports the *.MOV files as well. The following script, which is a slightly modified version,  should work for m15 as well, I didn't test it though.

```sh:
#!/bin/sh

DUALFISH_BLENDER=/Applications/RICOH\ THETA.app/Contents/Resources/tools/DualfishBlender.osx
SUFFIX=_er
if [ "${1}" = "dry" ]; then
  DRY=echo
fi

find . -name '*.MP4' -o -name '*.MOV' \
  | sed "s/\.MP4$//; s/\.MOV$//; s/${SUFFIX}$//;" \
  | sort \
  | uniq -c \
  | grep '^  *1 ' \
  | awk '{print $2}' \
  | xargs -n 1 -I % ${DRY} "${DUALFISH_BLENDER}" %.MOV %${SUFFIX}.MP4
```