You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. module Paperclip
  3. # This transcoder is only to be used for the MediaAttachment model
  4. # to check when uploaded videos are actually gifv's
  5. class Transcoder < Paperclip::Processor
  6. def initialize(file, options = {}, attachment = nil)
  7. super
  8. @current_format = File.extname(@file.path)
  9. @basename = File.basename(@file.path, @current_format)
  10. @format = options[:format]
  11. @time = options[:time] || 3
  12. @passthrough_options = options[:passthrough_options]
  13. @convert_options = options[:convert_options].dup
  14. end
  15. def make
  16. metadata = VideoMetadataExtractor.new(@file.path)
  17. unless metadata.valid?
  18. log("Unsupported file #{@file.path}")
  19. return File.open(@file.path)
  20. end
  21. update_attachment_type(metadata)
  22. update_options_from_metadata(metadata)
  23. destination = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
  24. destination.binmode
  25. @output_options = @convert_options[:output]&.dup || {}
  26. @input_options = @convert_options[:input]&.dup || {}
  27. case @format.to_s
  28. when /jpg$/, /jpeg$/, /png$/, /gif$/
  29. @input_options['ss'] = @time
  30. @output_options['f'] = 'image2'
  31. @output_options['vframes'] = 1
  32. when 'mp4'
  33. @output_options['acodec'] = 'aac'
  34. @output_options['strict'] = 'experimental'
  35. end
  36. command_arguments, interpolations = prepare_command(destination)
  37. begin
  38. command = Terrapin::CommandLine.new('ffmpeg', command_arguments.join(' '), logger: Paperclip.logger)
  39. command.run(interpolations)
  40. rescue Terrapin::ExitStatusError => e
  41. raise Paperclip::Error, "Error while transcoding #{@basename}: #{e}"
  42. rescue Terrapin::CommandNotFoundError
  43. raise Paperclip::Errors::CommandNotFoundError, 'Could not run the `ffmpeg` command. Please install ffmpeg.'
  44. end
  45. destination
  46. end
  47. private
  48. def prepare_command(destination)
  49. command_arguments = ['-nostdin']
  50. interpolations = {}
  51. interpolation_keys = 0
  52. @input_options.each_pair do |key, value|
  53. interpolation_key = interpolation_keys
  54. command_arguments << "-#{key} :#{interpolation_key}"
  55. interpolations[interpolation_key] = value
  56. interpolation_keys += 1
  57. end
  58. command_arguments << '-i :source'
  59. interpolations[:source] = @file.path
  60. @output_options.each_pair do |key, value|
  61. interpolation_key = interpolation_keys
  62. command_arguments << "-#{key} :#{interpolation_key}"
  63. interpolations[interpolation_key] = value
  64. interpolation_keys += 1
  65. end
  66. command_arguments << '-y :destination'
  67. interpolations[:destination] = destination.path
  68. [command_arguments, interpolations]
  69. end
  70. def update_options_from_metadata(metadata)
  71. return unless @passthrough_options && @passthrough_options[:video_codecs].include?(metadata.video_codec) && @passthrough_options[:audio_codecs].include?(metadata.audio_codec) && @passthrough_options[:colorspaces].include?(metadata.colorspace)
  72. @format = @passthrough_options[:options][:format] || @format
  73. @time = @passthrough_options[:options][:time] || @time
  74. @convert_options = @passthrough_options[:options][:convert_options].dup
  75. end
  76. def update_attachment_type(metadata)
  77. @attachment.instance.type = MediaAttachment.types[:gifv] unless metadata.audio_codec
  78. end
  79. end
  80. end