prefix = 'origin/' # 前缀(可看做目录/路径)
resp = Qiniu::Fop::Av.convert_to(prefix + 'n03-0620.mp4')
resp = Qiniu::Fop::Av.convert_to(prefix + 'Robi_tw.mp4')
resp = Qiniu::Fop::Av.convert_to(prefix + 'Robi_vs_jp.mp4')
Last active
November 4, 2015 07:48
-
-
Save mimosa/62b09ce77d76151e3967 to your computer and use it in GitHub Desktop.
Qiniu Ruby 视频转码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Qiniu | |
module Fop | |
module Av | |
class << self | |
def convert_to(key, opts={}) | |
opts.symbolize_keys! | |
opts.assert_valid_keys(:bucket, :rate, :pipeline, :callback) | |
rate = opts.delete(:rate) || '480p' # 码率 | |
bucket = opts.delete(:bucket) || 'episodes' # 空间名 | |
pipeline = opts.delete(:pipeline) || 'gsecret' # 队列名 | |
# 持久化处理结果通知 | |
# http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html#pfop-notification | |
callback = opts.delete(:callback) # 回调 url(队列执行完毕后,七牛可向此 url POST 执行结果) | |
# 这里采用 “处理结果另存” 的方式。 | |
# http://developer.qiniu.com/docs/v6/api/reference/fop/saveas.html | |
fname = to_vase64("#{bucket}:#{rate}/#{to_fname(key)}") | |
fops = rates[rate] + '|saveas/' + fname # | |
policy = Persistance::PfopPolicy.new(bucket, key, fops, callback) | |
policy.pipeline = pipeline | |
code, result, response_headers = Persistance.pfop(policy) | |
end | |
private | |
# 具体 “音视频” 处理参见: | |
# http://developer.qiniu.com/docs/v6/api/overview/fop/builtin-fop/av.html | |
def rates | |
{ '480p' => 'avthumb/mp4/ab/64k/acodec/libfaac/vb/1154k/vcodec/libx264/s/854x480/autoscale/1/stripmeta/1' } | |
end | |
def to_vase64(str) | |
Base64.urlsafe_encode64(str) | |
end | |
def to_fname(key) # 取原始文件名 | |
File.basename(key) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment