Dan*_*ele 1 ruby-on-rails amazon-s3 shrine amazon-elastic-transcoder
我正在开发一个需要上传视频的应用程序。我添加了 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
Run Code Online (Sandbox Code Playgroud)
转码正在工作,基本上是对上传到缓存文件夹的新文件进行转码,并放入同名的存储文件夹中。
现在的问题是将该文件附加到数据库中的记录中。截至目前,该记录已使用不同的名称进行更新,它会在 0mb 的存储文件夹中创建一个新文件。
如何将处理结果附加到Shrine上传的文件中进行存储?
该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
Run Code Online (Sandbox Code Playgroud)
如果您有机会为 Amazon Elastic Transcoder 制作 Shrine 插件,请查看shrine-transloadit,它为Transloadit提供集成,它使用与 Amazon Elastic Transcoder 几乎相同的流程,并且它与 webhooks 一起使用而不是轮询响应。
归档时间: |
|
查看次数: |
381 次 |
最近记录: |