3

我想使用 Shrine gem 上传视频文件、转码并从视频中生成缩略图。

我正在尝试将 Erik Dahlstrand 的Shrine-Rails-example从照片转换为视频。我在创建视频上传器时遇到问题。我将此代码基于Video is not of allowed type (allowed types: video/mp4), Shrine, Rails

require "streamio-ffmpeg"

class VideoUploader < Shrine
  ALLOWED_TYPES = %w[video/mp4 video/quicktime video/x-msvideo video/mpeg]
  plugin :processing
  plugin :versions

  plugin :determine_mime_type
  plugin :cached_attachment_data
  plugin :remove_attachment
  plugin :add_metadata
  plugin :validation_helpers
  plugin :derivation_endpoint, prefix: "derivations/video"

  add_metadata do |io, context|
    movie = Shrine.with_file(io) { |file| FFMPEG::Movie.new(file.path) }

    { "duration"   => movie.duration,
      "bitrate"    => movie.bitrate,
      "resolution" => movie.resolution,
      "frame_rate" => movie.frame_rate }
  end

  movie.screenshot("video_thumb_007.jpg", seek_time: 5, resolution: '320x240')

  metadata_method :duration

  Attacher.validate do
    validate_max_size 100.megabyte, message: "is too large (max is 100 MB)"
    validate_mime_type_inclusion ALLOWED_TYPES
  end
end

我收到此错误:

/var/folders/mm/_j8x4k2176jcv31zvbc497_c0000gp
    /T/shrine20190607-24438-4f3jz2.m4v: No such file or directory

事实上,该位置没有文件。那么等待上传时文件存储在哪里?

此外,使用演示将照片上传到 AWS(生产环境),对象存储在存储桶中名为“照片”的文件夹中。Shrine 显然使用表名来命名文件夹。是否可以创建替代和嵌套文件夹名称?

谢谢 - 似乎是一个了不起的宝石!试图更好地理解它!

谢谢

4

1 回答 1

2

进一步深入研究文档,我能够解决这个问题,只剩下一个问题 - 是否可以控制 AWS 存储桶中的哪些文件夹将原始文件和拇指上传到?谢谢

解决方案:

require "streamio-ffmpeg"
require "tempfile"

class VideoUploader < ImageUploader
  plugin :add_metadata
  plugin :validation_helpers
  plugin :processing
  plugin :versions
  plugin :delete_raw

  add_metadata do |io, context|
    movie = Shrine.with_file(io) { |file| FFMPEG::Movie.new(file.path) }
    { "duration"   => movie.duration,
      "bitrate"    => movie.bitrate,
      "resolution" => movie.resolution,
      "frame_rate" => movie.frame_rate
    }

  end

  process(:store) do |io, context|
    versions = {original: io}

    io.download do |original|
      screenshot1 = Tempfile.new(["screenshot1", ".jpg"], binmode: true)
      screenshot2 = Tempfile.new(["screenshot2", ".jpg"], binmode: true)
      screenshot3 = Tempfile.new(["screenshot3", ".jpg"], binmode: true)
      screenshot4 = Tempfile.new(["screenshot4", ".jpg"], binmode: true)

      movie = FFMPEG::Movie.new(original.path)
      movie.screenshot(screenshot1.path, seek_time: 5, resolution: '640x480')
      movie.screenshot(screenshot2.path, seek_time: 10, resolution: '640x480')
      movie.screenshot(screenshot3.path, seek_time: 15, resolution: '640x480')
      movie.screenshot(screenshot4.path, seek_time: 20, resolution: '640x480')

      [screenshot1, screenshot2, screenshot3, screenshot4].each(&:open) # refresh file descriptors

      versions.merge!(screenshot1: screenshot1, screenshot2: screenshot2, screenshot3: screenshot3, screenshot4: screenshot4)
    end

    versions
  end

  Attacher.validate do
    validate_max_size 100.megabyte, message: "is too large (max is 100 MB)"
    validate_mime_type_inclusion ALLOWED_TYPES
  end
end
于 2019-06-09T17:35:19.040 回答