lib/handbrake/cli.rb in handbrake-0.1.0 vs lib/handbrake/cli.rb in handbrake-0.2.0
- old
+ new
@@ -57,15 +57,92 @@
##
# Performs a conversion. This method immediately begins the
# transcoding process; set all other options first.
#
+ # @param [String] filename the desired name for the final output
+ # file
+ # @param [Hash] options additional options to control the behavior
+ # of the output process
+ # @option options [Boolean,:ignore] :overwrite (true) determines
+ # the behavior if the desired output file already exists. If
+ # `true`, the file is replaced. If `false`, an exception is
+ # thrown. If `:ignore`, the file is skipped; i.e., HandBrakeCLI
+ # is not invoked.
+ # @option options [Boolean] :atomic (false) provides a
+ # pseudo-atomic mode for transcoded output. If true, the
+ # transcode will go into a temporary file and only be copied to
+ # the specified filename if it completes. The temporary filename
+ # is the target filename with `.handbraking` inserted before the
+ # extension. Any `:overwrite` checking will be applied to the
+ # target filename both before and after the transcode happens
+ # (the temporary file will always be overwritten). This option
+ # is intended to aid in writing automatically resumable batch
+ # scripts.
+ #
# @return [void]
- def output(filename)
- run('--output', filename)
+ def output(filename, options={})
+ overwrite = options.delete :overwrite
+ case overwrite
+ when true, nil
+ # no special behavior
+ when false
+ raise FileExistsError, filename if File.exist?(filename)
+ when :ignore
+ if File.exist?(filename)
+ trace "Ignoring transcode to #{filename.inspect} because it already exists"
+ return
+ end
+ else
+ raise "Unsupported value for :overwrite: #{overwrite.inspect}"
+ end
+
+ atomic = options.delete :atomic
+ interim_filename =
+ if atomic
+ partial_filename(filename)
+ else
+ filename
+ end
+
+ unless options.empty?
+ raise "Unknown options for output: #{options.keys.inspect}"
+ end
+
+ run('--output', interim_filename)
+
+ if filename != interim_filename
+ replace =
+ if File.exist?(filename)
+ trace "#{filename.inspect} showed up during transcode"
+ case overwrite
+ when false
+ raise FileExistsError, filename
+ when :ignore
+ trace "- will leave #{filename.inspect} as is; copy #{interim_filename.inspect} manually if you want to replace it"
+ false
+ else
+ trace '- will replace with new transcode'
+ true
+ end
+ else
+ true
+ end
+ FileUtils.mv interim_filename, filename if replace
+ end
end
+ def partial_filename(name)
+ if File.basename(name).index '.'
+ dot_at = name.rindex '.'
+ name.dup.insert dot_at, '.handbraking'
+ else
+ name + '.handbraking'
+ end
+ end
+ private :partial_filename
+
##
# Performs a title scan. Unlike HandBrakeCLI, if you do not
# specify a title, this method will return information for all
# titles. (HandBrakeCLI defaults to only returning information for
# title 1.)
@@ -132,9 +209,13 @@
$stderr.puts result.output
end
raise "HandBrakeCLI execution failed (#{result.status.inspect})"
end
end
+ end
+
+ def trace(msg)
+ $stderr.puts msg if trace?
end
##
# Copies this CLI instance and appends another command line switch
# plus optional arguments.