forked from ultramango/gear360pano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gear360video.cmd
executable file
·208 lines (165 loc) · 5.33 KB
/
gear360video.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
:<<"::IGNORE_THIS_LINE"
@ECHO OFF
GOTO :CMDSCRIPT
::IGNORE_THIS_LINE
# This is a small script to stitch panorama videos produced
# by Samsung Gear360
#
# TODOs:
# - output file name is currently static
# - add check for gear360pano script
# - ffmpeg and jpg or png?
#
# Trick with Win/Linux from here:
# http://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash
################################ Linux part here
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in
# Or dirname `dirname $0`
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Clean-up function
function clean_up {
echo "Removing temporary directories..."
if [ -d "FRAMESTMPDIR" ]; then
rm -rf "FRAMESTMPDIR"
fi
if [ -d "OUTTEMPDIR" ]; then
rm -rf "OUTTEMPDIR"
fi
}
# Function to check if a command fails
# http://stackoverflow.com/questions/5195607/checking-bash-exit-status-of-several-commands-efficiently
function run_command {
"$@"
local status=$?
if [ $status -ne 0 ]; then
echo "Error while running $1" >&2
clean_up
exit 1
fi
return $status
}
# Do stuff to make this thing run on various operating systems
# http://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux
function os_check {
case "$(uname -s)" in
Darwin)
;;
Linux)
;;
CYGWIN*|MINGW32*|MSYS*)
# Naive approach, should read Hugin installation
# path from registry or something...
export PATH=$PATH:"/cygdrive/c/Program Files (x86)/Hugin/bin":"/cygdrive/c/Program Files/Hugin/bin"
;;
*)
;;
esac
}
# Check argument(s)
if [ -z "$1" ]; then
echo "Small script to stitch video panorama files."
echo -e "Script originally writen for Samsung Gear 360.\n"
echo -e "Usage:\n$0 inputdir [outputfile]\n"
echo "Where inputfile is a panorama file from camera,"
echo "output parameter is optional."
exit 1
fi
# Output name as second argument
if [ -z "$2" ]; then
OUTNAME=`basename "${1%.*}"`_pano.mp4
echo "DEBUG: output filename: $OUTNAME"
fi
# OS check, custom settings for various OSes
os_check
# Check if we have the software to do it
# http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
type ffmpeg >/dev/null 2>&1 || { echo >&2 "ffmpeg required but it's not installed. Aborting."; exit 1; }
# TODO: add check for gear360pano.cmd script
# Create temporary directory locally to stay compatible with other OSes
FRAMESTEMPDIR=`mktemp -d -p .`
OUTTEMPDIR=`mktemp -d -p .`
IMAGETMPL="image%05d.jpg"
STARTTS=`date +%s`
# Stitch panorama (same file twice as input)
echo "Extracting frames from video (this might take a while..."
cmd="ffmpeg -i $1 -vf scale=7776:3888 $FRAMESTEMPDIR/$IMAGETMPL"
run_command $cmd
echo "Stitching frames..."
for i in $FRAMESTEMPDIR/*.jpg; do
echo Frame: $i
OUTFILENAME=`basename $i`
cmd="/bin/bash ./gear360pano.cmd $i $OUTTEMPDIR/$OUTFILENAME"
run_command $cmd
done
echo "Recoding the video..."
cmd="ffmpeg -f image2 -i $OUTTEMPDIR/$IMAGETMPL -r 30 -s 3840:1920 -vcodec libx264 $OUTNAME"
run_command $cmd
# Remove temporary directories
clean_up
# Inform user about the result
ENDTS=`date +%s`
RUNTIME=$((ENDTS-STARTTS))
echo Video written to $OUTNAME, took: $RUNTIME s
exit 0
################################ Windows part here
:CMDSCRIPT
set FFMPEGPATH=c:/Program Files/ffmpeg/bin
set FRAMESTEMPDIR=frames
set OUTTEMPDIR=frames_stitched
:: %% is an escape character
set IMAGETMPL=image%%05d.jpg
:: Check arguments
IF [%1] == [] GOTO NOARGS
:: Check if second argument present, if not, set some default for output filename
IF NOT [%2] == [] GOTO SETNAMEOK
set OUTNAME="%~n1_pano.mp4"
:SETNAMEOK
:: Where's enblend? Prefer 64 bits
if exist "%FFMPEGPATH%/ffmpeg.exe" goto FFMPEGOK
:: 64 bits not found? Check x86
goto NOFFMPEG
:FFMPEGOK
:: Create temporary directories
mkdir %FRAMESTEMPDIR%
mkdir %OUTTEMPDIR%
:: Execute commands (as simple as it is)
echo Converting video to images...
"%FFMPEGPATH%/ffmpeg.exe" -i %1 -vf scale=7776:3888 %FRAMESTEMPDIR%/%IMAGETMPL%
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
:: Stitching
echo Stitching frames...
for %%f in (%FRAMESTEMPDIR%/*.jpg) do (
:: For whatever reason (this has to be at the beginning of the line!)
echo Processing frame %FRAMESTEMPDIR%\%%f
:: There should be some error checking
call gear360pano.cmd %FRAMESTEMPDIR%\%%f %OUTTEMPDIR%\%%f
)
"%FFMPEGPATH%/ffmpeg.exe" -f image2 -i %OUTTEMPDIR%/%IMAGETMPL% -r 30 -s 3840:1920 -vcodec libx264 %OUTNAME%
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
:: Clean-up (f - force, read-only & dirs, q - quiet)
del /f /q %FRAMESTEMPDIR%
del /f /q %OUTTEMPDIR%
echo Video written to %OUTNAME%
goto END
:NOARGS
echo Small script to stitch raw video panorama files.
echo Raw meaning two fisheye images side by side.
echo Script originally writen for Samsung Gear 360.
echo.
echo Usage:
echo %0 inputfile [outputfile]
echo.
echo Where inputfile is a panorama file from camera,
echo output parameter is optional
goto END
:NOFFMPEG
echo ffmpeg is found in %FFMPEGPATH%, download from: https://ffmpeg.zeranoe.com/builds/ and
echo unpack to program files directory
goto END
:FFMPEGERROR
echo ffmpeg failed, video not created
goto END
:STITCHINGERROR
echo Stitching failed, video not created
goto END
:END