带有神社的亚马逊弹性转码
amazon elastic transcode with shrine
我正在开发一款需要上传视频的应用。我添加了 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 的存储文件夹中创建了一个新文件。
如何将处理结果附加到神社上传的文件中保存?
process(:store)
块希望您 return 一个文件供 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 which provides integration for Transloadit,它使用与 Amazon Elastic Transcoder 几乎相同的流程,并且可以与webhooks 而不是轮询响应。
我正在开发一款需要上传视频的应用。我添加了 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 的存储文件夹中创建了一个新文件。
如何将处理结果附加到神社上传的文件中保存?
process(:store)
块希望您 return 一个文件供 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 which provides integration for Transloadit,它使用与 Amazon Elastic Transcoder 几乎相同的流程,并且可以与webhooks 而不是轮询响应。