transcoder.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. @vfr_threshold = options[:vfr_frame_rate_threshold]
  15. end
  16. def make
  17. metadata = VideoMetadataExtractor.new(@file.path)
  18. unless metadata.valid?
  19. Paperclip.log("Unsupported file #{@file.path}")
  20. return File.open(@file.path)
  21. end
  22. update_attachment_type(metadata)
  23. update_options_from_metadata(metadata)
  24. destination = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
  25. destination.binmode
  26. @output_options = @convert_options[:output]&.dup || {}
  27. @input_options = @convert_options[:input]&.dup || {}
  28. case @format.to_s
  29. when /jpg$/, /jpeg$/, /png$/, /gif$/
  30. @input_options['ss'] = @time
  31. @output_options['f'] = 'image2'
  32. @output_options['vframes'] = 1
  33. when 'mp4'
  34. @output_options['acodec'] = 'aac'
  35. @output_options['strict'] = 'experimental'
  36. if high_vfr?(metadata) && !eligible_to_passthrough?(metadata)
  37. @output_options['vsync'] = 'vfr'
  38. @output_options['r'] = @vfr_threshold
  39. end
  40. end
  41. command_arguments, interpolations = prepare_command(destination)
  42. begin
  43. command = Terrapin::CommandLine.new('ffmpeg', command_arguments.join(' '), logger: Paperclip.logger)
  44. command.run(interpolations)
  45. rescue Terrapin::ExitStatusError => e
  46. raise Paperclip::Error, "Error while transcoding #{@basename}: #{e}"
  47. rescue Terrapin::CommandNotFoundError
  48. raise Paperclip::Errors::CommandNotFoundError, 'Could not run the `ffmpeg` command. Please install ffmpeg.'
  49. end
  50. destination
  51. end
  52. private
  53. def prepare_command(destination)
  54. command_arguments = ['-nostdin']
  55. interpolations = {}
  56. interpolation_keys = 0
  57. @input_options.each_pair do |key, value|
  58. interpolation_key = interpolation_keys
  59. command_arguments << "-#{key} :#{interpolation_key}"
  60. interpolations[interpolation_key] = value
  61. interpolation_keys += 1
  62. end
  63. command_arguments << '-i :source'
  64. interpolations[:source] = @file.path
  65. @output_options.each_pair do |key, value|
  66. interpolation_key = interpolation_keys
  67. command_arguments << "-#{key} :#{interpolation_key}"
  68. interpolations[interpolation_key] = value
  69. interpolation_keys += 1
  70. end
  71. command_arguments << '-y :destination'
  72. interpolations[:destination] = destination.path
  73. [command_arguments, interpolations]
  74. end
  75. def update_options_from_metadata(metadata)
  76. return unless eligible_to_passthrough?(metadata)
  77. @format = @passthrough_options[:options][:format] || @format
  78. @time = @passthrough_options[:options][:time] || @time
  79. @convert_options = @passthrough_options[:options][:convert_options].dup
  80. end
  81. def high_vfr?(metadata)
  82. @vfr_threshold && metadata.r_frame_rate && metadata.r_frame_rate > @vfr_threshold
  83. end
  84. def eligible_to_passthrough?(metadata)
  85. @passthrough_options && @passthrough_options[:video_codecs].include?(metadata.video_codec) && @passthrough_options[:audio_codecs].include?(metadata.audio_codec) && @passthrough_options[:colorspaces].include?(metadata.colorspace)
  86. end
  87. def update_attachment_type(metadata)
  88. @attachment.instance.type = MediaAttachment.types[:gifv] unless metadata.audio_codec
  89. end
  90. end
  91. end