gif_transcoder.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # frozen_string_literal: true
  2. class GifReader
  3. attr_reader :animated
  4. EXTENSION_LABELS = [0xf9, 0x01, 0xff].freeze
  5. GIF_HEADERS = %w(GIF87a GIF89a).freeze
  6. class GifReaderException < StandardError; end
  7. class UnknownImageType < GifReaderException; end
  8. class CannotParseImage < GifReaderException; end
  9. def self.animated?(path)
  10. new(path).animated
  11. rescue GifReaderException
  12. false
  13. end
  14. def initialize(path, max_frames = 2)
  15. @path = path
  16. @nb_frames = 0
  17. File.open(path, 'rb') do |s|
  18. raise UnknownImageType unless GIF_HEADERS.include?(s.read(6))
  19. # Skip to "packed byte"
  20. s.seek(4, IO::SEEK_CUR)
  21. # "Packed byte" gives us the size of the GIF color table
  22. packed_byte, = s.read(1).unpack('C')
  23. # Skip background color and aspect ratio
  24. s.seek(2, IO::SEEK_CUR)
  25. if packed_byte & 0x80 != 0
  26. # GIF uses a global color table, skip it
  27. s.seek(3 * (1 << ((packed_byte & 0x07) + 1)), IO::SEEK_CUR)
  28. end
  29. # Now read data
  30. while @nb_frames < max_frames
  31. separator = s.read(1)
  32. case separator
  33. when ',' # Image block
  34. @nb_frames += 1
  35. # Skip to "packed byte"
  36. s.seek(8, IO::SEEK_CUR)
  37. packed_byte, = s.read(1).unpack('C')
  38. if packed_byte & 0x80 != 0
  39. # Image uses a local color table, skip it
  40. s.seek(3 * (1 << ((packed_byte & 0x07) + 1)), IO::SEEK_CUR)
  41. end
  42. # Skip lzw min code size
  43. raise InvalidValue unless s.read(1).unpack('C')[0] >= 2
  44. # Skip image data sub-blocks
  45. skip_sub_blocks!(s)
  46. when '!' # Extension block
  47. skip_extension_block!(s)
  48. when ';' # Trailer
  49. break
  50. else
  51. raise CannotParseImage
  52. end
  53. end
  54. end
  55. @animated = @nb_frames > 1
  56. end
  57. private
  58. def skip_extension_block!(file)
  59. if EXTENSION_LABELS.include?(file.read(1).unpack('C')[0])
  60. block_size, = file.read(1).unpack('C')
  61. file.seek(block_size, IO::SEEK_CUR)
  62. end
  63. # Read until extension block end marker
  64. skip_sub_blocks!(file)
  65. end
  66. # Skip sub-blocks up until block end marker
  67. def skip_sub_blocks!(file)
  68. loop do
  69. size, = file.read(1).unpack('C')
  70. break if size.zero?
  71. file.seek(size, IO::SEEK_CUR)
  72. end
  73. end
  74. end
  75. module Paperclip
  76. # This transcoder is only to be used for the MediaAttachment model
  77. # to convert animated gifs to webm
  78. class GifTranscoder < Paperclip::Processor
  79. def make
  80. return File.open(@file.path) unless needs_convert?
  81. final_file = Paperclip::Transcoder.make(file, options, attachment)
  82. attachment.instance.file_file_name = File.basename(attachment.instance.file_file_name, '.*') + '.mp4'
  83. attachment.instance.file_content_type = 'video/mp4'
  84. attachment.instance.type = MediaAttachment.types[:gifv]
  85. final_file
  86. end
  87. private
  88. def needs_convert?
  89. options[:style] == :original && GifReader.animated?(file.path)
  90. end
  91. end
  92. end