1

我正在开发一款需要上传视频的应用程序。我添加了神社和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的上传文件中进行存储?

回答

1

process(:store)块需要您返回一个Shrine文件上传到永久存储器,因此Amazon Elastic Transcoder不适用于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 

如果你的任何机会感兴趣的制作靖国神社插件亚马逊弹性转码器,看看shrine-transloadit提供集成Transloadit,它使用几乎相同的流作为Amazon Elastic Transcoder,它可以与webhooks协作,而不是轮询响应。