transcoder.rb 3.9 KB

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