|
5 | 5 |
|
6 | 6 | FIXED_LOWER_TO_UPPER_RATIO = 16.0/9.0 |
7 | 7 | FIXED_UPPER_TO_LOWER_RATIO = 9.0/16.0 |
| 8 | +TEMP_DIR = "/tmp/rlovelett".freeze |
8 | 9 |
|
9 | 10 | module FFMPEG |
10 | 11 | class Transcoder |
@@ -36,16 +37,18 @@ def initialize(movie, output_file, options = EncodingOptions.new, transcoder_opt |
36 | 37 | if requires_pre_encode |
37 | 38 | @movie.paths.each do |path| |
38 | 39 | # Make the interim path folder if it doesn't exist |
39 | | - dirname = "#{File.dirname(path)}/interim" |
| 40 | + |
| 41 | + dirname = "#{TEMP_DIR}/interim#{File.dirname(path)}/" |
40 | 42 | unless File.directory?(dirname) |
41 | 43 | FileUtils.mkdir_p(dirname) |
42 | 44 | end |
43 | | - |
44 | | - interim_path = "#{File.dirname(path)}/interim/#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4" |
| 45 | + # Example output: /tmp/rlovelett/interim/test_gv6Hw86ryqklKNiYCu9a8w.mp4 |
| 46 | + interim_path = "#{dirname}#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4" |
45 | 47 | @movie.interim_paths << interim_path |
46 | 48 | end |
47 | 49 | else |
48 | 50 | @movie.interim_paths << @movie.paths |
| 51 | + @movie.interim_paths.flatten! |
49 | 52 | end |
50 | 53 |
|
51 | 54 | if options.is_a?(String) |
@@ -171,7 +174,14 @@ def delete_files(destination) |
171 | 174 | def transcode_movie |
172 | 175 | pre_encode_if_necessary |
173 | 176 |
|
174 | | - @command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(@output_file)}" |
| 177 | + temp_output_dir = "#{TEMP_DIR}/output/" |
| 178 | + FileUtils.mkdir_p(temp_output_dir) unless File.directory?(temp_output_dir) |
| 179 | + |
| 180 | + # We use a temporary output file to avoid issues with manipulating the output file directly in ffmpeg, which can occur due to s3 mountpoint restrictions. |
| 181 | + # Example output: /tmp/rlovelett/output/test_gv6Hw86ryqklKNiYCu9a8w.mp4 |
| 182 | + temp_output_file = "#{temp_output_dir}#{File.basename(@output_file, File.extname(@output_file))}_#{SecureRandom.urlsafe_base64}#{File.extname(@output_file)}" |
| 183 | + |
| 184 | + @command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(temp_output_file)}" |
175 | 185 |
|
176 | 186 | FFMPEG.logger.info("Running transcoding...\n#{@command}\n") |
177 | 187 | @output = "" |
@@ -204,6 +214,16 @@ def transcode_movie |
204 | 214 | raise Error, "Process hung. Full output: #{@output}" |
205 | 215 | end |
206 | 216 | end |
| 217 | + |
| 218 | + # Copy temp_output_file to output_file and cleanup interim paths |
| 219 | + if File.exist?(temp_output_file) |
| 220 | + FileUtils.cp(temp_output_file, @output_file) |
| 221 | + FileUtils.rm_rf(temp_output_file) |
| 222 | + end |
| 223 | + ensure |
| 224 | + @movie.interim_paths.each do |path| |
| 225 | + FileUtils.rm_rf(path) if path.start_with?(TEMP_DIR) |
| 226 | + end |
207 | 227 | end |
208 | 228 |
|
209 | 229 | def validate_output_file(&block) |
|
0 commit comments