Skip to content

Commit

Permalink
add tmp dir to process the file
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesPurvis committed Oct 17, 2024
1 parent 20e2f37 commit 7aac56a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/ffmpeg/transcoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def initialize(movie, output_file, options = EncodingOptions.new, transcoder_opt
if requires_pre_encode
@movie.paths.each do |path|
# Make the interim path folder if it doesn't exist
dirname = "#{File.dirname(path)}/interim"
dirname = "/tmp/interim"
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end

interim_path = "#{File.dirname(path)}/interim/#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4"
interim_path = "/tmp/interim/#{File.basename(path, File.extname(path))}_#{SecureRandom.urlsafe_base64}.mp4"
@movie.interim_paths << interim_path
end
else
Expand Down Expand Up @@ -170,8 +170,10 @@ def delete_files(destination)

def transcode_movie
pre_encode_if_necessary

@command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(@output_file)}"
# change output file to /tmp/interim/output.mp4 needs to be unique to every run
# get file extension from original file - dont overwrite original file
temp_output_file = "/tmp/interim/#{File.basename(@output_file, File.extname(@output_file))}_#{SecureRandom.urlsafe_base64}#{File.extname(@output_file)}"
@command = "#{@movie.ffmpeg_command} -y #{@raw_options} #{Shellwords.escape(temp_output_file)}"

FFMPEG.logger.info("Running transcoding...\n#{@command}\n")
@output = ""
Expand Down Expand Up @@ -204,6 +206,10 @@ def transcode_movie
raise Error, "Process hung. Full output: #{@output}"
end
end
# copy temp output file to original output file
FileUtils.cp(temp_output_file, @output_file)
# delete temp output file
FileUtils.rm_rf(temp_output_file, secure: true)
end

def validate_output_file(&block)
Expand Down

0 comments on commit 7aac56a

Please sign in to comment.