@@ -17,36 +17,39 @@ GOTO :CMDSCRIPT
17
17
################################ Linux part here
18
18
19
19
# 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`)
22
21
23
22
# Clean-up function
24
- function clean_up {
23
+ clean_up() {
25
24
echo " Removing temporary directories..."
26
- if [ -d " FRAMESTMPDIR" ]; then
27
- rm -rf " FRAMESTMPDIR"
25
+ if [ -d " $ FRAMESTMPDIR" ]; then
26
+ rm -rf " $ FRAMESTMPDIR"
28
27
fi
29
- if [ -d " OUTTEMPDIR" ]; then
30
- rm -rf " OUTTEMPDIR"
28
+ if [ -d " $ OUTTEMPDIR" ]; then
29
+ rm -rf " $ OUTTEMPDIR"
31
30
fi
32
31
}
33
32
34
33
# Function to check if a command fails
35
34
# http://stackoverflow.com/questions/5195607/checking-bash-exit-status-of-several-commands-efficiently
36
- function run_command {
35
+ run_command() {
37
36
" $@"
38
37
local status=$?
39
38
if [ $status -ne 0 ]; then
40
39
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
41
44
clean_up
42
45
exit 1
43
46
fi
44
47
return $status
45
48
}
46
49
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
48
51
# http://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux
49
- function os_check {
52
+ os_check() {
50
53
case " $(uname -s)" in
51
54
52
55
Darwin)
@@ -67,17 +70,20 @@ function os_check {
67
70
68
71
# Check argument(s)
69
72
if [ -z " $1" ]; then
70
- echo " Small script to stitch video panorama files ."
73
+ echo " Small script to stitch panoramic videos ."
71
74
echo -e " Script originally writen for Samsung Gear 360.\n"
72
75
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 ,"
74
77
echo " output parameter is optional."
78
+ run_command notify-send " Please provide an input file."
79
+ sleep 2
75
80
exit 1
76
81
fi
77
82
78
83
# Output name as second argument
79
84
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
81
87
echo " DEBUG: output filename: $OUTNAME"
82
88
fi
83
89
@@ -89,28 +95,42 @@ os_check
89
95
type ffmpeg > /dev/null 2 >& 1 || { echo >& 2 " ffmpeg required but it's not installed. Aborting." ; exit 1; }
90
96
# TODO: add check for gear360pano.cmd script
91
97
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
93
99
FRAMESTEMPDIR=`mktemp -d -p .`
94
- OUTTEMPDIR=`mktemp -d -p . `
100
+ OUTTEMPDIR=`mktemp -d`
95
101
IMAGETMPL=" image%0 5d.jpg"
102
+ TMPAUDIO=" tmpaudio.aac"
103
+ TMPVIDEO=" tmpvideo.mp4"
96
104
STARTTS=`date +%s`
97
105
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"
102
111
112
+ # Stitch frames
103
113
echo " Stitching frames..."
104
114
for i in $FRAMESTEMPDIR/*.jpg; do
105
115
echo Frame: $i
106
116
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"
109
118
done
110
119
120
+ # Put stitched frames together
111
121
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"
114
134
115
135
# Remove temporary directories
116
136
clean_up
@@ -119,7 +139,7 @@ clean_up
119
139
ENDTS=`date +%s`
120
140
RUNTIME=$((ENDTS-STARTTS))
121
141
echo Video written to $OUTNAME, took: $RUNTIME s
122
-
142
+ run_command notify-send " 'Conversion complete. Video written to $OUTNAME, took: $RUNTIME s' "
123
143
exit 0
124
144
125
145
################################ Windows part here
@@ -129,21 +149,18 @@ exit 0
129
149
set FFMPEGPATH = c:/Program Files/ffmpeg/bin
130
150
set FRAMESTEMPDIR = frames
131
151
set OUTTEMPDIR = frames_stitched
132
- :: %% is an escape character
152
+ :: %% is an escape character (note: this will fail on wine's cmd.exe)
133
153
set IMAGETMPL = image%% 05d.jpg
154
+ set TMPAUDIO = tmpaudio.aac
155
+ set TMPVIDEO = tmpvideo.mp4
134
156
135
157
:: Check arguments
136
158
IF [%1 ] == [] GOTO NOARGS
137
159
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
-
142
160
:SETNAMEOK
143
161
144
- :: Where's enblend? Prefer 64 bits
162
+ :: Check ffmpeg...
145
163
if exist " %FFMPEGPATH% /ffmpeg.exe" goto FFMPEGOK
146
- :: 64 bits not found? Check x86
147
164
goto NOFFMPEG
148
165
149
166
:FFMPEGOK
@@ -154,19 +171,37 @@ mkdir %OUTTEMPDIR%
154
171
155
172
:: Execute commands (as simple as it is)
156
173
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%
158
175
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
159
176
160
177
:: Stitching
161
178
echo Stitching frames...
162
179
for %%f in (%FRAMESTEMPDIR% /*.jpg) do (
163
180
:: For whatever reason (this has to be at the beginning of the line!)
164
181
echo Processing frame %FRAMESTEMPDIR% \%%f
165
- :: There should be some error checking
182
+ :: TODO: There should be some error checking
166
183
call gear360pano.cmd %FRAMESTEMPDIR% \%%f %OUTTEMPDIR% \%%f
167
184
)
168
185
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%
170
205
if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
171
206
172
207
:: Clean-up (f - force, read-only & dirs, q - quiet)
@@ -191,18 +226,13 @@ goto END
191
226
192
227
:NOFFMPEG
193
228
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
196
231
goto END
197
232
198
233
:FFMPEGERROR
199
234
200
235
echo ffmpeg failed, video not created
201
236
goto END
202
237
203
- :STITCHINGERROR
204
-
205
- echo Stitching failed, video not created
206
- goto END
207
-
208
238
:END
0 commit comments