關於音頻、視頻等多媒體資本,七牛雲也供應了雄厚的處置懲罰指令,包含但不限於以下指令:
- 音視頻元信息(avinfo)
- 基礎音視頻處置懲罰(avthumb)
- 視頻截圖(vframe)
- 視頻水印(vwatermark)
- 音視頻切片(avthumb/m3u8/segment)
- 私有M3U8(pm3u8)
本篇從獵取音視頻元信息入手,遞次解說各個處置懲罰指令。
■ 獵取音視頻元信息
運用avinfo接口能夠異常方便地獵取一個音視頻資本的相關元信息:
http://<Bucket>.qiniudn.com/<Key>?avinfo
或
http://<Domain>/<Key>?avinfo
以美劇《黑名單》第1季第12集的預告片(flv資本)為例,在瀏覽器中翻開以下URL:
http://qiniu-developer.u.qiniudn.com/samples/黑名單-S01E12.flv?avinfo
將返回一個JSON花樣組織的元信息對象:
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"codec_type": "video",
"codec_time_base": "1001/60000",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 1280,
"height": 720,
...省略太長內容...
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "Advanced Audio Coding",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "s16",
"sample_rate": "44100",
"channels": 2,
...省略太長內容...
}
],
"format": {
"nb_streams": 2,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime/MPEG-4/Motion JPEG 2000 format",
"start_time": "0.000000",
"duration": "29.070000",
"size": "8702170",
"bit_rate": "2394818",
"tags": {
"major_brand": "mp42",
"minor_version": "0",
"compatible_brands": "isommp42",
"creation_time": "2014-01-13 08:43:21"
}
}
}
能夠看到音頻、視頻和封裝花樣信息被正確地形貌出來。
■ 基礎音視頻處置懲罰
avthumb接口支撐的基礎音視頻處置懲罰包含:
- 轉換編碼(如h264轉x264,mp3轉aac);
- 轉換封裝花樣(如flv轉mp4)
- 截取片斷;
- 修正編碼碼率;
- 修正分辨率。
以前文的flv資本為例,若只想簡樸地轉碼為mp4花樣,能夠運用以下URL殺青目的:
http://qiniu-developer.u.qiniudn.com/samples/黑名單-S01E12.flv?avthumb/mp4
吸收到如許的要求后,七牛雲將對指定資本實行實時轉碼操縱,緩存結果后將新資本返回給要求端。點擊檢察轉碼結果。
注重:
- avthumb接口是同步接口,如原資本過大,將致使接見端超時返回,強烈建議運用預轉耐久化處置懲罰接口或觸發耐久化處置懲罰接口舉行預處置懲罰,加速接見速率;
- 為加速接見速率,轉碼后的結果還將被七牛雲緩存起來,不計入存儲空間,節約盤算資本,逾期失效後會從新觸發盤算。
■ 預轉耐久化處置懲罰
上傳時,經由過程在上傳戰略中指定persistentOps字段的值,能夠觸發七牛雲對上傳資本舉行指定的數據處置懲罰。還能夠同時指定persistentNotifyUrl字段的值,以便將耐久化處置懲罰結果實時關照給營業端處置懲罰。
以前文的flv資本為例,若想預先轉換成mp4花樣並耐久存儲結果(計入存儲空間),能夠運用以下兩個Ruby順序來完成。
- [淺易HTTP服務器] 吸收預轉耐久化處置懲罰的結果,並將狀況信息打印到終端上:
#!/usr/bin/env ruby
# encoding : utf-8
# persistent_notify_server.rb
require 'json'
require 'xmlrpc/httpserver'
class PersistentNotifyHandler
@@count = 0
public
def ip_auth_handler(io)
# 任何要求都許可處置懲罰
return true
end # ip_auth_handler
def request_handler(request, response)
# 讀取要求報文
body = request.data.read_nonblock(65536)
# 從新花樣化JSON對象
json = JSON.generate(
JSON.parse(body),
{
indent: ' ',
object_nl: "\n",
array_nl: "\n",
}
)
# 輸出
puts json
# 計數
@@count += 1
# 組織相應報文(可選)
response.body = 'OK'
end # request_handler
def self.count()
return @@count
end # self.count
end # PersistentNotifyHandler
svr = HttpServer.new(
PersistentNotifyHandler.new(), # 要求處置懲罰器
9090, # 端口
'0.0.0.0' # 監聽IP
)
svr.start
while (PersistentNotifyHandler.count() == 0)
puts 'waiting for notification...'
sleep(60)
end
svr.shutdown
- [HTTP客戶端] 上傳flv文件並觸發預轉耐久化處置懲罰:
#!/usr/bin/env ruby
# encoding : utf-8
# put_flv_file.rb
require 'json'
require 'net/http'
require 'base64'
require 'openssl'
# 依據傳入參數,組織一個上傳戰略(觸發預轉耐久化處置懲罰)
def put_policy(bucket, expires, persistentOps, persistentNotifyUrl)
# 天生一個Hash對象
put_policy = Hash.new()
# 僅指定目的存儲空間,即“新增資本”語意:
# 資本不存在則建立
# 資本已存在,且與上傳內容不一致則失利
put_policy['scope'] = "#{bucket}"
# 盤算受權有效期停止時候,UNIX時候戳花樣
put_policy['deadline'] = (Time.now() + expires).tv_sec()
# 指定預轉耐久化處置懲罰的指令
put_policy['persistentOps'] = persistentOps
# 指定預轉耐久化處置懲罰的結果關照URL
put_policy['persistentNotifyUrl'] = persistentNotifyUrl
# 序列化為JSON字符串
return JSON.generate(put_policy)
end # put_policy
# 依據傳入的上傳戰略,天生對應的上傳受權憑據
def upload_token(access_key, secret_key, put_policy)
# 對上傳戰略做UrlSafe-Base64編碼
encoded_put_policy = Base64.urlsafe_encode64(put_policy)
# 運用SHA1作為HASH函數,天生簽名
sign = OpenSSL::HMAC.digest(
'sha1',
secret_key,
encoded_put_policy
)
# 對簽名做UrlSafe-Base64編碼
encoded_sign = Base64.urlsafe_encode64(sign)
# 拼出上傳受權憑據,以“:”作為分隔符
return "#{access_key}:#{encoded_sign}:#{encoded_put_policy}"
end # upload_token
BUCKET = 'qiniu-ts-demo' # 運用時請替換成實在的存儲空間名
EXPIRES = 3600
ACCESS_KEY = 'MY_ACCESS_KEY' # 運用時請替換成實在的AccessKey
SECRET_KEY = 'MY_SECRET_KEY' # 運用時請替換成實在的SecretKey
PERSISTENT_OPS = 'avthumb/mp4' # 耐久化處置懲罰指令
PERSISTENT_NOTIFY_URL = 'http://fake.com:9090' # 運用時請替換成實在的域名和端口
# 天生上傳受權憑據
upload_token = upload_token(
ACCESS_KEY,
SECRET_KEY,
put_policy(
BUCKET,
EXPIRES,
PERSISTENT_OPS,
PERSISTENT_NOTIFY_URL
)
)
# 指定要求報文中的各個參數
file_name = '黑名單-S01E12.flv' # 運用時請替換成實在的文件
file_content = File.open(file_name, 'rb') do |fh|
fh.read()
end
puts 'file size is %s' % file_content.size
boundary = 'a-string-never-exists-in-the-uploading-file'
# 天生要求報體裁
req_body = <<HTTP_BODY
--#{boundary}
Content-Disposition: form-data; name="token"
#{upload_token}
--#{boundary}
Content-Disposition: form-data; name="key"
#{file_name}
--#{boundary}
Content-Disposition: form-data; name="file"; filename="#{file_name}"
Content-Type: video/x-flv
Content-Transfer-Encoding: binary
HTTP_BODY
# 轉換換行符
req_body.gsub!(/\n/, "\r\n")
req_body = req_body.force_encoding('ASCII-8BIT') + file_content + "\r\n--#{boundary}--\r\n"
# 天生Headers
req_headers = Hash.new()
req_headers['Host'] = "up.qiniu.com"
req_headers['Content-Type'] = "multipart/form-data; boundary=#{boundary}"
req_headers['Content-Length'] = "#{req_body.size}"
# 發送要求
http_client = Net::HTTP.new('up.qiniu.com', 80)
resp = http_client.post(
'/',
req_body,
req_headers
)
# 剖析相應
puts "HTTP Code=#{resp.code}"
puts "HTTP Msg=#{resp.msg}"
puts "HTTP Body=#{resp.body()}"
先啟動服務器,然後實行上傳順序,守候一段時候后便可收到預轉勝利的關照結果:
[Mon Jan 20 19:10:19 2014] HttpServer 0.0.0.0:9090 client:39573 115.238.138.231<115.238.138.231> connect
{
"id":"16i99r7gjlrc8r9213",
"code":0,
"desc":"The fop was completed successfully",
"items":[
{
"cmd":"avthumb/mp4",
"code":0,
"desc":"The fop was completed successfully",
"error":"",
"hash":"lpqijRaQ4c_CPoKDL1bLWK7TUoI3",
"key":"UAA-4hndfVc5V6DJX0EvslAUBBI=/ll8spobyuu_F112ZWyG6Va4qk4Ch"
}
]
}
[Mon Jan 20 19:10:19 2014] HttpServer 0.0.0.0:9090 client:39573 disconnect
[Mon Jan 20 19:10:52 2014] HttpServer 0.0.0.0:9090 stop
个中,Key字段給出耐久化的mp4資本的名字,即能夠經由過程以下URL接見轉換好的mp4視頻:
http://qiniu-ts-demo.qiniudn.com/UAA-4hndfVc5V6DJX0EvslAUBBI=/ll8spobyuu_F112ZWyG6Va4qk4Ch
點擊檢察轉碼結果。
■ 我們行進到哪兒了?
經由過程三個實例,開端解說了音視頻處置懲罰的基礎接口(avinfo/avthumb),讀者應該有才能自行編程完成上傳與觸發預轉耐久化處置懲罰。
七牛雲存儲 © 2014 簽名-非商業性運用-制止歸納
許可自在轉載,請說明作者及出處。