Skip to content

Commit 6e93c6b

Browse files
authored
Added sound merging (contribution by OWKenobi)
1 parent 667e894 commit 6e93c6b

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

gear360video.cmd

+71-41
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,39 @@ GOTO :CMDSCRIPT
1717
################################ Linux part here
1818

1919
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in
20-
# Or dirname `dirname $0`
21-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
20+
DIR=$(dirname `which $0`)
2221

2322
# Clean-up function
24-
function clean_up {
23+
clean_up() {
2524
echo "Removing temporary directories..."
26-
if [ -d "FRAMESTMPDIR" ]; then
27-
rm -rf "FRAMESTMPDIR"
25+
if [ -d "$FRAMESTMPDIR" ]; then
26+
rm -rf "$FRAMESTMPDIR"
2827
fi
29-
if [ -d "OUTTEMPDIR" ]; then
30-
rm -rf "OUTTEMPDIR"
28+
if [ -d "$OUTTEMPDIR" ]; then
29+
rm -rf "$OUTTEMPDIR"
3130
fi
3231
}
3332

3433
# Function to check if a command fails
3534
# http://stackoverflow.com/questions/5195607/checking-bash-exit-status-of-several-commands-efficiently
36-
function run_command {
35+
run_command() {
3736
"$@"
3837
local status=$?
3938
if [ $status -ne 0 ]; then
4039
echo "Error while running $1" >&2
40+
if [ $1 != "notify-send" ]; then
41+
# Display error in a nice graphical popup if available
42+
run_command notify-send "Error while running $1"
43+
fi
4144
clean_up
4245
exit 1
4346
fi
4447
return $status
4548
}
4649

47-
# Do stuff to make this thing run on various operating systems
50+
# Do stuff to make this thing run on various POSIX operating systems
4851
# http://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux
49-
function os_check {
52+
os_check() {
5053
case "$(uname -s)" in
5154

5255
Darwin)
@@ -67,17 +70,20 @@ function os_check {
6770

6871
# Check argument(s)
6972
if [ -z "$1" ]; then
70-
echo "Small script to stitch video panorama files."
73+
echo "Small script to stitch panoramic videos."
7174
echo -e "Script originally writen for Samsung Gear 360.\n"
7275
echo -e "Usage:\n$0 inputdir [outputfile]\n"
73-
echo "Where inputfile is a panorama file from camera,"
76+
echo "Where inputfile is a panoramic video file,"
7477
echo "output parameter is optional."
78+
run_command notify-send "Please provide an input file."
79+
sleep 2
7580
exit 1
7681
fi
7782

7883
# Output name as second argument
7984
if [ -z "$2" ]; then
80-
OUTNAME=`basename "${1%.*}"`_pano.mp4
85+
# If invoked by nautilus open-with, we need to remember the proper directory in the outname
86+
OUTNAME=`dirname "$1"`/`basename "${1%.*}"`_pano.mp4
8187
echo "DEBUG: output filename: $OUTNAME"
8288
fi
8389

@@ -89,28 +95,42 @@ os_check
8995
type ffmpeg >/dev/null 2>&1 || { echo >&2 "ffmpeg required but it's not installed. Aborting."; exit 1; }
9096
# TODO: add check for gear360pano.cmd script
9197

92-
# Create temporary directory locally to stay compatible with other OSes
98+
# On some systems not using '-p .' (temp in current dir) might cause problems
9399
FRAMESTEMPDIR=`mktemp -d -p .`
94-
OUTTEMPDIR=`mktemp -d -p .`
100+
OUTTEMPDIR=`mktemp -d`
95101
IMAGETMPL="image%05d.jpg"
102+
TMPAUDIO="tmpaudio.aac"
103+
TMPVIDEO="tmpvideo.mp4"
96104
STARTTS=`date +%s`
97105

98-
# Stitch panorama (same file twice as input)
99-
echo "Extracting frames from video (this might take a while..."
100-
cmd="ffmpeg -i $1 -vf scale=7776:3888 $FRAMESTEMPDIR/$IMAGETMPL"
101-
run_command $cmd
106+
# Extract frames from video
107+
# TODO: currently frames are upscaled, use dedicated .pto file
108+
run_command notify-send 'Starting panoramic video stitching...'
109+
echo "Extracting frames from video (this might take a while)..."
110+
run_command ffmpeg -y -i "$1" -vf scale=7776:3888 "$FRAMESTEMPDIR/$IMAGETMPL"
102111

112+
# Stitch frames
103113
echo "Stitching frames..."
104114
for i in $FRAMESTEMPDIR/*.jpg; do
105115
echo Frame: $i
106116
OUTFILENAME=`basename $i`
107-
cmd="/bin/bash ./gear360pano.cmd $i $OUTTEMPDIR/$OUTFILENAME"
108-
run_command $cmd
117+
run_command "/bin/bash" "$DIR/gear360pano.cmd" "$i" "$OUTTEMPDIR/$OUTFILENAME"
109118
done
110119

120+
# Put stitched frames together
111121
echo "Recoding the video..."
112-
cmd="ffmpeg -f image2 -i $OUTTEMPDIR/$IMAGETMPL -r 30 -s 3840:1920 -vcodec libx264 $OUTNAME"
113-
run_command $cmd
122+
run_command ffmpeg -y -f image2 -i "$OUTTEMPDIR/$IMAGETMPL" -r 30 -s 3840:1960 -vcodec libx264 "$OUTTEMPDIR/$TMPVIDEO"
123+
124+
#use this for medium size
125+
#run_command ffmpeg -y -f image2 -i "$OUTTEMPDIR/$IMAGETMPL" -r 30 -s 1920:960 -vcodec libx264 "$OUTTEMPDIR/$TMPVIDEO"
126+
127+
echo "Extracting audio..."
128+
run_command notify-send "Extracting audio..."
129+
run_command ffmpeg -y -i "$1" -vn -acodec copy "$OUTTEMPDIR/$TMPAUDIO"
130+
131+
echo "Merging audio..."
132+
run_command notify-send "Merging audio..."
133+
run_command ffmpeg -y -i "$OUTTEMPDIR/$TMPVIDEO" -i "$OUTTEMPDIR/$TMPAUDIO" -c:v copy -c:a aac -strict experimental "$OUTNAME"
114134

115135
# Remove temporary directories
116136
clean_up
@@ -119,7 +139,7 @@ clean_up
119139
ENDTS=`date +%s`
120140
RUNTIME=$((ENDTS-STARTTS))
121141
echo Video written to $OUTNAME, took: $RUNTIME s
122-
142+
run_command notify-send "'Conversion complete. Video written to $OUTNAME, took: $RUNTIME s'"
123143
exit 0
124144

125145
################################ Windows part here
@@ -129,21 +149,18 @@ exit 0
129149
set FFMPEGPATH=c:/Program Files/ffmpeg/bin
130150
set FRAMESTEMPDIR=frames
131151
set OUTTEMPDIR=frames_stitched
132-
:: %% is an escape character
152+
:: %% is an escape character (note: this will fail on wine's cmd.exe)
133153
set IMAGETMPL=image%%05d.jpg
154+
set TMPAUDIO=tmpaudio.aac
155+
set TMPVIDEO=tmpvideo.mp4
134156

135157
:: Check arguments
136158
IF [%1] == [] GOTO NOARGS
137159

138-
:: Check if second argument present, if not, set some default for output filename
139-
IF NOT [%2] == [] GOTO SETNAMEOK
140-
set OUTNAME="%~n1_pano.mp4"
141-
142160
:SETNAMEOK
143161

144-
:: Where's enblend? Prefer 64 bits
162+
:: Check ffmpeg...
145163
if exist "%FFMPEGPATH%/ffmpeg.exe" goto FFMPEGOK
146-
:: 64 bits not found? Check x86
147164
goto NOFFMPEG
148165

149166
:FFMPEGOK
@@ -154,19 +171,37 @@ mkdir %OUTTEMPDIR%
154171

155172
:: Execute commands (as simple as it is)
156173
echo Converting video to images...
157-
"%FFMPEGPATH%/ffmpeg.exe" -i %1 -vf scale=7776:3888 %FRAMESTEMPDIR%/%IMAGETMPL%
174+
"%FFMPEGPATH%/ffmpeg.exe" -y -i %1 -vf scale=7776:3888 %FRAMESTEMPDIR%/%IMAGETMPL%
158175
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
159176

160177
:: Stitching
161178
echo Stitching frames...
162179
for %%f in (%FRAMESTEMPDIR%/*.jpg) do (
163180
:: For whatever reason (this has to be at the beginning of the line!)
164181
echo Processing frame %FRAMESTEMPDIR%\%%f
165-
:: There should be some error checking
182+
:: TODO: There should be some error checking
166183
call gear360pano.cmd %FRAMESTEMPDIR%\%%f %OUTTEMPDIR%\%%f
167184
)
168185

169-
"%FFMPEGPATH%/ffmpeg.exe" -f image2 -i %OUTTEMPDIR%/%IMAGETMPL% -r 30 -s 3840:1920 -vcodec libx264 %OUTNAME%
186+
echo "Reencoding video..."
187+
"%FFMPEGPATH%/ffmpeg.exe" -y -f image2 -i %OUTTEMPDIR%/%IMAGETMPL% -r 30 -s 3840:1920 -vcodec libx264 %OUTTEMPDIR%/%TMPVIDEO%
188+
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
189+
190+
echo "Extracting audio..."
191+
"%FFMPEGPATH%/ffmpeg.exe" -y -i %1 -vn -acodec copy %OUTTEMPDIR%/%TMPAUDIO%
192+
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
193+
194+
echo "Merging audio..."
195+
196+
:: Check if second argument present, if not, set some default for output filename
197+
:: This is here, because for whatever reason OUTNAME gets overriden by
198+
:: the last iterated filename if this is at the beginning (for loop is buggy?)
199+
if not [%2] == [] goto SETNAMEOK
200+
set OUTNAME="%~n1_pano.mp4"
201+
202+
:SETNAMEOK
203+
204+
"%FFMPEGPATH%/ffmpeg.exe" -y -i %OUTTEMPDIR%/%TMPVIDEO% -i %OUTTEMPDIR%/%TMPAUDIO% -c:v copy -c:a aac -strict experimental %OUTNAME%
170205
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
171206

172207
:: Clean-up (f - force, read-only & dirs, q - quiet)
@@ -191,18 +226,13 @@ goto END
191226

192227
:NOFFMPEG
193228

194-
echo ffmpeg is found in %FFMPEGPATH%, download from: https://ffmpeg.zeranoe.com/builds/ and
195-
echo unpack to program files directory
229+
echo ffmpeg was not found in %FFMPEGPATH%, download from: https://ffmpeg.zeranoe.com/builds/
230+
echo and unpack to program files directory
196231
goto END
197232

198233
:FFMPEGERROR
199234

200235
echo ffmpeg failed, video not created
201236
goto END
202237

203-
:STITCHINGERROR
204-
205-
echo Stitching failed, video not created
206-
goto END
207-
208238
:END

0 commit comments

Comments
 (0)