【问题标题】:amazon elastic transcode with shrine亚马逊弹性转码与神社
【发布时间】:2017-08-25 23:48:12
【问题描述】:

我正在开发一个需要上传视频的应用。我添加了 Shrine 和 s3 存储。

到这里一切正常。现在我需要对视频进行转码,并将以下代码添加到 video_uploader 文件中

class VideoUploader < Shrine

  plugin :processing
  plugin :versions

  process(:store) do |io|

    transcoder = Aws::ElasticTranscoder::Client.new(
      access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region:            'us-east-1',
    )

    pipeline = transcoder.create_pipeline(options = {
        :name => "name",
        :input_bucket => "bucket",
        :output_bucket => "bucket",
        :role => "arn:aws:iam::XXXXX:role/Elastic_Transcoder_Default_Role",
    })

    PIPELINE_ID =  pipeline[:pipeline][:id]

    transcode_hd = transcoder.create_job({
      :pipeline_id=>PIPELINE_ID,
      :input=> {
        :key=> "cache/"+io.id,
        :frame_rate=> "auto",
        :resolution => "auto",
        :aspect_ratio => "auto",
        :container => 'auto'
      },
      :outputs=>[{
        :key=>"store/"+io.id,
        :preset_id=>"1351620000001-000010",
      }]
    })

  end

end 

转码正在工作,基本上是对上传到缓存文件夹的新文件进行转码,并放入同名的存储文件夹中。

现在的问题是将此文件附加到数据库中的记录。截至目前,记录已更新为不同的名称,它会在 0mb 的存储文件夹中创建一个新文件。

如何将处理结果附加到Shrine上传的文件中存储?

【问题讨论】:

    标签: ruby-on-rails amazon-s3 shrine amazon-elastic-transcoder


    【解决方案1】:

    process(:store) 块希望您返回一个文件供 Shrine 上传到永久存储,因此此流程将无法使用 Amazon Elastic Transcoder,因为 Amazon Elastic Transcoder 现在是将缓存文件上传到永久存储的流程.

    您可以将转码请求延迟到后台作业中,每 N 秒轮询一次转码作业,并从结果中创建一个Shrine::UploadedFile 并更新记录。像下面这样的东西应该可以工作:

    # superclass for all uploaders that use Amazon Elastic Transcoder
    class TranscoderUploader < Shrine
      plugin :backgrounding
      Attacher.promote { |data| TranscodeJob.perform_async(data) }
    end
    
    class VideoUploader < TranscoderUploader
      plugin :versions
    end
    
    class TranscodeJob
      include Sidekiq::Worker
    
      def perform(data)
        attacher = TranscoderUploader::Attacher.load(data)
        cached_file = attacher.get #=> #<Shrine::UploadedFile>
    
        # create transcoding job, use `cached_file.id`
    
        transcoder.wait_until(:job_complete, id: job.id)
        response = transcoder.read_job(id: job.id)
        output = response.output
    
        versions = {
          video: attacher.shrine_class::UploadedFile.new(
            "id" => cached_file.id,
            "storage" => "store",
            "metadata" => {
              "width" => output.width,
              "height" => output.height,
              # ...
            }
          ),
          ...
        }
    
        attacher.swap(versions)
      end
    end
    

    如果您有兴趣为 Amazon Elastic Transcoder 制作 Shrine 插件,请查看 shrine-transloadit,它提供了与 Transloadit 的集成,它使用与 Amazon Elastic Transcoder 几乎相同的流程,以及它适用于 webhook,而不是轮询响应。

    【讨论】:

      猜你喜欢
      • 2014-06-06
      • 2023-03-31
      • 2012-08-19
      • 2011-08-12
      • 1970-01-01
      • 2012-07-28
      • 2019-05-08
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多