ruby-on-rails - 从 AWS Elastic Transcoder 作业中检索文件和缩略图 URL

标签 ruby-on-rails video amazon-web-services amazon-s3 amazon-sns

我有一个 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"=>"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}

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

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

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

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

最佳答案

缩略图的实际名称似乎没有从 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 倍。

关于ruby-on-rails - 从 AWS Elastic Transcoder 作业中检索文件和缩略图 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624144/

相关文章:

python - 使用 Pymongo 从 Windows 连接到 AWS 实例上的 MongoDB

ruby-on-rails - 你如何将 'save' 数据写入 Rails Controller 中的数据库?

ruby-on-rails - Ruby:使用私钥/公钥进行文件加密/解密

ruby-on-rails - Ruby on Rails : how do I use a default placeholder thing in a select_tag?

c++ - 使用 DirectShow 按请求读取帧

html - mp4 视频无法在 iphone 上播放;盒子是空白的

使用 FFmpeg 编码的视频播放速度过快

mysql - has_many 总和事件记录

amazon-web-services - 在特定子网中创建 EKS kubernetes pod

amazon-web-services - 验证并使用 iAM 用户访问 S3