从 AWS Elastic Transcoder 作业中检索文件和缩略图 url [英] Retrieve file and thumbnail url from AWS Elastic Transcoder job

查看:32
本文介绍了从 AWS Elastic Transcoder 作业中检索文件和缩略图 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,它使用 CORS 配置将视频上传到 AWS S3 存储桶,当完成并创建 rails 视频对象时,会创建一个 Elastic Transcoder 作业以将视频编码为 .mp4 格式并生成缩略图图像,AWS SNS 已启用以在作业完成时发送推送通知.

该过程一切正常,上传完成后我会收到 SNS 通知,但是我可以很好地获取视频网址,但通知仅包含缩略图模式而不是实际文件名.

以下是我从 AWS SNS 收到的典型通知.注意.这是来自输出哈希

{"id"=>"1", "presetId"=>"1351620000001-000040", "key"=>"uploads/video/150/557874e9-4c67-40f0-8f98-8c59506647e5/IMG_0587.mp4", "thumbnailPattern"=>"上传/视频/150/557874e9-4c67-40f0-8f98-8c59506647e5/{count},"stat">auto=">"Complete", "statusDetail"=>"转码作业完成.", "duration"=>10, "width"=>202, "height"=>360}

如您所见,thumbnailPattern 只是要使用的文件模式,而不是创建的实际文件.

有谁知道我如何获得通过弹性转码器和 SNS 创建的文件的 URL?

transcoder.rb # => 我在保存视频后创建一个新的转码器对象

类转码器<视频定义初始化(视频)@video = 视频@directory = "上传/视频/#{@video.id}/#{SecureRandom.uuid}/"@filename = File.basename(@video.file, File.extname(@video.file))结尾定义创建transcoder = AWS::ElasticTranscoder::Client.new(region: "us-east-1")选项 = {管道 ID:配置 [:aws_pipeline_id],输入: {key: @video.file.split("/")[3..-1].join("/"), # 切掉amazon.com 位帧率:自动",分辨率:'自动',纵横比:'自动',隔行扫描:'自动',容器:'自动'},输出: [{键:#{@filename}.mp4",预设 ID:'1351620000001-000040',旋转:自动",缩略图模式:{count}#{@filename}"}],output_key_prefix: "#{@directory}"}作业 = transcoder.create_job(options)@video.job_id = job.data[:job][:id]@video.save!结尾结尾

VideosController #create

class VideosController <应用控制器定义创建@video = current_user.videos.build(params[:video])response_to do |格式|如果@video.savetranscode = Transcoder.new(@video)转码创建format.html { redirect_to videos_path,注意:'视频已成功上传.'}format.json { 渲染 json: @video, 状态: :created, location: @video }格式.js别的format.html { 渲染动作:新"}format.json { 渲染 json: @video.errors, 状态: :unprocessable_entity }结尾结尾结尾结尾

解决方案

缩略图的实际名称似乎没有从 SNS 通知或创建作业时的请求响应中传回:

http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/create-job.html#create-job-examples

http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/notifications.html

因为您的缩略图的基本路径/名称是已知的,并且序列号将始终从 00001 开始,您可以从那里迭代以确定在作业完成时是否存在/有多少缩略图存在.确保对 S3 中的对象使用 HEAD 请求来确定它们的存在;它比执行 LIST 请求大约便宜 10 倍.

I have a rails app which uploads videos to an AWS S3 bucket using their CORS configuration, when this is completed and the rails video object is created an Elastic Transcoder job is created to encode the video to .mp4 format and generate a thumbnail image, AWS SNS is enabled to send push notifications when the job is complete.

The process all works nicely and I receive a SNS notification when the upload is complete, however I can fetch the video url just fine but the notification only contains the thumbnail pattern rather than the actual filename.

Below is a typical notification I receive from AWS SNS. NB. This is from the outputs hash

{"id"=>"1", "presetId"=>"1351620000001-000040", "key"=>"uploads/video/150/557874e9-4c67-40f0-8f98-8c59506647e5/IMG_0587.mp4", "thumbnailPattern"=>"uploads/video/150/557874e9-4c67-40f0-8f98-8c59506647e5/{count}IMG_0587", "rotate"=>"auto", "status"=>"Complete", "statusDetail"=>"The transcoding job is completed.", "duration"=>10, "width"=>202, "height"=>360}

As you can see under thumbnailPattern is just the filepattern to use, and not the actual file created.

Does anyone know how I can get the URLS to the files created over elastic transcoder and SNS?

transcoder.rb # => I create a new transcoder object when a video has been saved

class Transcoder < Video
  def initialize(video)
    @video = video
    @directory = "uploads/video/#{@video.id}/#{SecureRandom.uuid}/"
    @filename = File.basename(@video.file, File.extname(@video.file))
  end

  def create
    transcoder = AWS::ElasticTranscoder::Client.new(region: "us-east-1")
    options = {
      pipeline_id: CONFIG[:aws_pipeline_id],
      input: { 
        key: @video.file.split("/")[3..-1].join("/"), # slice off the amazon.com bit 
        frame_rate: "auto", 
        resolution: 'auto', 
        aspect_ratio: 'auto', 
        interlaced: 'auto', 
        container: 'auto' 
        },
      outputs: [
        {
          key: "#{@filename}.mp4", 
          preset_id: '1351620000001-000040', 
          rotate: "auto", 
          thumbnail_pattern: "{count}#{@filename}"
        }
      ],
      output_key_prefix: "#{@directory}"
    }
    job = transcoder.create_job(options)
    @video.job_id = job.data[:job][:id]
    @video.save!
  end
end

VideosController #create

class VideosController < ApplicationController
  def create
    @video = current_user.videos.build(params[:video])
    respond_to do |format|
      if @video.save
        transcode = Transcoder.new(@video)
        transcode.create
        format.html { redirect_to videos_path, notice: 'Video was successfully uploaded.' }
        format.json { render json: @video, status: :created, location: @video }
        format.js 
      else
        format.html { render action: "new" }
        format.json { render json: @video.errors, status: :unprocessable_entity }
      end
    end
  end
end

解决方案

It doesn't appear that the actual name of the thumbnails are passed back, either from SNS notifications or from the request response upon creation of a job:

http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/create-job.html#create-job-examples

http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/notifications.html

Because the base path/name of your thumbnails is known, and the sequence number will always start at 00001, you can iterate from there to determine if/how many of the thumbnails exist upon job completion. Ensure you use HEAD requests against the objects in S3 to determine their presence; its about 10x cheaper than doing a LIST request.

这篇关于从 AWS Elastic Transcoder 作业中检索文件和缩略图 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆