使用Serverless Devs開發工具可以快速部署基於Function Compute、Object Storage Service和FFmpeg的應用,協助您處理音視頻,例如擷取音視頻資訊、給音視頻添加浮水印及轉換格式等。
背景資訊
FFmpeg是一套可以記錄、轉換數字音視頻,並將其轉化為流的開源電腦程式。FFmpeg採用LGPL或GPL許可證,提供了錄製、轉換和流化音視頻的完整解決方案,包括先進的音視頻編解碼庫libavcodec,並且保證了高可移植性和編解碼品質。詳細資料,請參見FFmpeg。
本文以Python語言為例,介紹的樣本模板中包含以下主題。您也可以根據實際需求修改給到樣本的函數代碼實現二次開發。
樣本名稱 | 描述 |
GetMediaMeta | 擷取音視頻的Meta資訊。 |
GetDuration | 擷取音視頻的時間長度。 |
GetSprites | 將視頻製作成雪碧圖。 |
VideoWatermark | 添加文字浮水印、靜態圖片浮水印和動態GIF浮水印。 |
AudioConvert | 轉換音視頻格式。 |
VideoGif | 將Video格式提取成GIF格式。 |
前提條件
使用Serverless Devs部署應用
執行以下命令,初始化專案。
sudo s init ffmpeg-app -d ffmpeg-app
-d
用於指定產生的目錄的名稱。執行以下命令,進入專案目錄。
cd ffmpeg-app
可選:您可以按需修改專案目錄中的程式碼範例,實現您的商務邏輯。
執行以下命令,部署專案。
sudo s deploy -y
說明如果您只需要部署該專案內的某個功能,例如需要部署GetMediaMeta功能擷取音視頻的Meta資訊。請執行以下命令:
sudo s GetMediaMeta deploy
當您需要部署其他功能時,請將
GetMediaMeta
修改為其他功能的名稱。部署完成後,輸出樣本如下。
[2021-11-25T17:35:56.524] [INFO ] [S-CLI] - Start ... [2021-11-25T17:35:56.529] [INFO ] [S-CLI] - It is detected that your project has the following projects < AudioConvert,GetMediaMeta,GetDuration,VideoGif,GetSprites,VideoWatermark > to be execute [2021-11-25T17:35:56.530] [INFO ] [S-CLI] - Start executing project AudioConvert [2021-11-25T17:35:57.725] [INFO ] [FC-DEPLOY] - Using region: cn-qingdao [2021-11-25T17:35:57.725] [INFO ] [FC-DEPLOY] - Using access alias: default [2021-11-25T17:35:57.726] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9**** [2021-11-25T17:35:57.726] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch. ...... There is auto config in the service: FcOssFFmpeg ...... AudioConvert: region: cn-qingdao service: name: FcOssFFmpeg function: name: AudioConvert runtime: python3 handler: index.handler memorySize: 256 timeout: 600 GetMediaMeta: region: cn-qingdao service: name: FcOssFFmpeg function: name: GetMediaMeta runtime: python3 handler: index.handler memorySize: 1024 timeout: 600 GetDuration: region: cn-qingdao service: name: FcOssFFmpeg function: name: GetDuration runtime: python3 handler: index.handler memorySize: 256 timeout: 600 VideoGif: region: cn-qingdao service: name: FcOssFFmpeg function: name: VideoGif runtime: python3 handler: index.handler memorySize: 512 timeout: 600 GetSprites: region: cn-qingdao service: name: FcOssFFmpeg function: name: GetSprites runtime: python3 handler: index.handler memorySize: 512 timeout: 600 VideoWatermark: region: cn-qingdao service: name: FcOssFFmpeg function: name: VideoWatermark runtime: python3 handler: index.handler memorySize: 256 timeout: 600
調用樣本函數。
調用GetMediaMeta函數,擷取音視頻的Meta資訊
調用GetMediaMeta函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s GetMediaMeta invoke -e '{"bucket_name": "test-bucket","object_key": "a.mp4"}'
參數說明:
bucket_name
:Bucket名稱。object_key
:需擷取音視頻Meta資訊的檔案的名稱。
輸出樣本:
[2021-11-26T14:19:02.045] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FunctionCompute python3 runtime inited. FC Invoke Start RequestId: dda964e0-82b6-452a-b849-6b0b835f**** 2021-11-26T06:19:04.688Z dda964e0-82b6-452a-b849-6b0b835f**** [INFO] current Function [handler] excute time is 0.23 seconds FC Invoke End RequestId: dda964e0-82b6-452a-b849-6b0b835f**** Duration: 1238.78 ms, Billed Duration: 1239 ms, Memory Size: 1024 MB, Max Memory Used: 118.39 MB ========= FC invoke Logs end ========= FC Invoke Result: { "format": { "bit_rate": "17024829", "duration": "110.037333", "filename": "http://test-bucket.oss-cn-qingdao-internal.aliyuncs.com/a.mp4......", "format_long_name": "QuickTime / MOV", "format_name": "mov,mp4,m4a,3gp,3g2,mj2", "nb_programs": 0, "nb_streams": 2, "probe_score": 100, "size": "234170850", "start_time": "0.000000", "tags": { "compatible_brands": "mp42mp41", "creation_time": "2020-09-05T06:03:49.000000Z", "major_brand": "mp42", "minor_version": "0" } }, "streams": [ { "avg_frame_rate": "25/1", "bit_rate": "16708594", "bits_per_raw_sample": "8", "chroma_location": "left", "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", "codec_name": "h264", "codec_tag": "0x31637661", "codec_tag_string": "avc1", "codec_time_base": "1/50", "codec_type": "video", "coded_height": 1088, "coded_width": 1920, "color_primaries": "bt709", "color_range": "tv", "color_space": "bt709", "color_transfer": "bt709", "disposition": { "attached_pic": 0, "clean_effects": 0, "comment": 0, "default": 1, "dub": 0, "forced": 0, "hearing_impaired": 0, "karaoke": 0, "lyrics": 0, "original": 0, "timed_thumbnails": 0, "visual_impaired": 0 }, "duration": "110.000000", "duration_ts": 2750000, "has_b_frames": 1, "height": 1080, "index": 0, "is_avc": "true", "level": 41, "nal_length_size": "4", "nb_frames": "2750", "pix_fmt": "yuv420p", "profile": "Main", "r_frame_rate": "25/1", "refs": 1, "start_pts": 0, "start_time": "0.000000", "tags": { "creation_time": "2020-09-05T06:03:49.000000Z", "encoder": "AVC Coding", "handler_name": "\u001fMainconcept Video Media Handler", "language": "eng" }, "time_base": "1/25000", "width": 1920 }, { "avg_frame_rate": "0/0", "bit_rate": "317375", "bits_per_sample": 0, "channel_layout": "stereo", "channels": 2, "codec_long_name": "AAC (Advanced Audio Coding)", "codec_name": "aac", "codec_tag": "0x6134706d", "codec_tag_string": "mp4a", "codec_time_base": "1/48000", "codec_type": "audio", "disposition": { "attached_pic": 0, "clean_effects": 0, "comment": 0, "default": 1, "dub": 0, "forced": 0, "hearing_impaired": 0, "karaoke": 0, "lyrics": 0, "original": 0, "timed_thumbnails": 0, "visual_impaired": 0 }, "duration": "110.000000", "duration_ts": 5280000, "index": 1, "max_bit_rate": "417750", "nb_frames": "5158", "profile": "LC", "r_frame_rate": "0/0", "sample_fmt": "fltp", "sample_rate": "48000", "start_pts": 0, "start_time": "0.000000", "tags": { "creation_time": "2020-09-05T06:03:49.000000Z", "handler_name": "#Mainconcept MP4 Sound Media Handler", "language": "eng" }, "time_base": "1/48000" } ] } End of method: invoke
調用GetDuration函數,擷取音視頻的時間長度
調用GetDuration函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s GetDuration invoke -e '{"bucket_name": "bucket-name","object_key": "a.mp4"}'
參數說明:
bucket_name
:Bucket名稱。object_key
:需擷取時間長度的檔案的名稱。
輸出樣本:
[2021-11-26T14:21:48.877] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FunctionCompute python3 runtime inited. FC Invoke Start RequestId: 6bb9ecae-7f53-4efb-afea-7614ef87**** 2021-11-26T06:21:50.273Z 6bb9ecae-7f53-4efb-afea-7614ef87**** [INFO] current Function [handler] excute time is 0.17 seconds FC Invoke End RequestId: 6bb9ecae-7f53-4efb-afea-7614ef87**** Duration: 754.63 ms, Billed Duration: 755 ms, Memory Size: 256 MB, Max Memory Used: 61.21 MB ========= FC invoke Logs end ========= FC Invoke Result: 110.037333 End of method: invoke
調用GetSprites函數,將視頻製作成雪碧圖
調用GetSprites函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s GetSprites invoke -e '{"bucket_name": "test-bucket","object_key": "aclear.mp4", "output_dir" : "output/", "tile": "3*4"}'
參數說明:
bucket_name
:Bucket的名稱。object_key
:需製作成雪碧圖的檔案的名稱。output_dir
:轉碼後的視頻儲存在Bucket內的位置。tile
:雪碧圖的行和列。(可選)
start
:開始製作雪碧圖的時間。預設值為0。(可選)
duration
:基於Start參數後多長時間的視頻內開始截圖。例如start取值為10,duration取值為20,表示截取10s到30s內的視頻。(可選)
itsoffset
:表示流位移命令,延遲時間。預設值為0。該參數需要和start、interval一起使用,樣本如下:start
取值為0,interval
取值為10,itsoffset
取值為0,則截圖的描述為5、15和25等。start
取值為0,interval
取值為10,itsoffset
取值為1,則截圖的描述為4、14和24等。start
取值為0,interval
取值為10,itsoffset
取值為-1,則截圖的描述為6、16和26等。start
取值為0,interval
取值為10,itsoffset
取值為4.999,則截圖的描述為0、10和20等。
說明當該參數取值為5時,會丟失0秒時的圖片,建議您將該參數設定為4.999。
(可選)
scale
:截圖的大小。預設比例為-1:-1。(可選)
interval
:表示時隔幾秒截取一次視頻。預設值為1。(可選)
padding
:表示圖片的間隔。預設值為0。(可選)
color
:表示雪碧圖的背景顏色。預設顏色為黑色。(可選)
dst_type
:表示產生雪碧圖的圖片格式。預設為JPG。取值為JPG和PNG。
輸出樣本:
[2021-11-26T16:07:42.585] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FunctionCompute python3 runtime inited. FC Invoke Start RequestId: 1b427831-e10f-4c2b-b780-9b504c29aa67 2021-11-26T08:07:44.684Z 1b427831-e10f-4c2b-b780-9b504c29aa67 [INFO] b'{"bucket_name": "test-bucket","object_key": "a.mp4", "output_dir" : "output/", "dst_type":".wav"}' 2021-11-26T08:07:51.642Z 1b427831-e10f-4c2b-b780-9b504c29aa67 [INFO] Uploaded /tmp/transcoded_a.wav to output/transcoded_a.wav 2021-11-26T08:07:51.642Z 1b427831-e10f-4c2b-b780-9b504c29aa67 [INFO] current Function [handler] excute time is 6.96 seconds FC Invoke End RequestId: 1b427831-e10f-4c2b-b780-9b504c29aa67 Duration: 7876.26 ms, Billed Duration: 7877 ms, Memory Size: 3072 MB, Max Memory Used: 119.06 MB ========= FC invoke Logs end ========= FC Invoke Result: ok End of method: invoke
調用VideoWatermark函數,給視頻添加浮水印
調用VideoWatermark函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s VideoWatermark invoke -e '{"bucket_name": "test-bucket","object_key": "a.mp4", "output_dir" : "output/", "vf_args" : "drawtext=fontfile=/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc:text='helloFunction Compute':x=100:y=50:fontsize=24:fontcolor=red"}'
參數說明:
bucket_name
:Bucket的名稱。object_key
:需添加浮水印的檔案的名稱。output_dir
:轉碼後的視頻儲存在Bucket內的位置。vf_args
:表示文字浮水印或靜態圖片浮水印。filter_complex_args
:表示動態圖片浮水印。當設定該參數與vf_args
同時設定時,則預設忽略vf_args
的參數資訊。
輸出樣本:
[2021-11-26T15:20:24.396] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= static-master/target/lib ...... ...... FC Invoke End RequestId: 31ecddfa-4e41-44bb-9489-00708b07**** Duration: 1302.08 ms, Billed Duration: 1303 ms, Memory Size: 256 MB, Max Memory Used: 256.00 MB ========= FC invoke Logs end ========= FC Invoke Result: ok End of method: invoke
調用AudioConvert函數,轉換音頻格式
調用AudioConvert函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s AudioConvert invoke -e '{"bucket_name": "test-bucket","object_key": "a.mp4", "output_dir" : "output/", "dst_type":".wav", "ac":"1", "ar":"4000"}'
參數說明:
bucket_name
:Bucket的名稱。object_key
:需轉換音頻格式的檔案的名稱。output_dir
:轉碼後的視頻儲存在Bucket內的位置。dst_type
:轉換後檔案的格式。(可選)
ac
:指定聲道數。(可選)
ar
:指定採樣率。
輸出樣本:
[2021-11-26T16:04:16.293] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= ...... 2021-11-26T08:04:18.520Z 2fc578cd-8787-4681-ab21-a3f6b4ab1e2a [ERROR] returncode:1 ...... Duration: 1156.09 ms, Billed Duration: 1157 ms, Memory Size: 256 MB, Max Memory Used: 88.23 MB ========= FC invoke Logs end ========= FC Invoke Result: ok End of method: invoke
調用VideoGif函數,為視頻提取GIF
調用VideoGif函數的樣本命令如下,請根據實際情況修改參數資訊。
sudo s VideoGif invoke -e '{"bucket_name": "test-bucket","object_key": "a.mp4", "output_dir" : "output/", "vframes": "5", "start": "0", "duration": "2"}'
參數說明:
bucket_name
:Bucket的名稱。object_key
:需轉換為GIF格式的檔案的名稱。output_dir
:轉碼後的視頻儲存在Object Storage Service中的位置。(可選)
vframes
:基於start
參數後多長時間的視頻內開始轉換視頻。(可選)
start
:開始轉換的時間。預設值為0。(可選)
duration
:基於start
參數後多長時間的視頻內開始轉換視頻。
說明當同時設定了
duration
和vframes
參數時,以duration
的參數資訊為準。當這兩個參數都未設定時,則預設將整個視頻轉換為GIF格式。輸出樣本:
[2021-11-26T15:27:26.647] [INFO ] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FunctionCompute python3 runtime inited. FC Invoke Start RequestId: a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb 2021-11-26T07:27:28.279Z a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb [INFO] b'{"bucket_name": "test-bucket","object_key": "a.mp4", "output_dir" : "output/", "vframes": "5", "start": "0", "duration": "2"}' 2021-11-26T07:27:28.280Z a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb [INFO] cmd = ffmpeg -y -ss 0 -t 2 -accurate_seek -i ...... 2021-11-26T07:27:30.150Z a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb [INFO] Uploaded /tmp/a.gif to output/a.gif 2021-11-26T07:27:30.151Z a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb [INFO] current Function [handler] excute time is 1.87 seconds FC Invoke End RequestId: a49fc8b4-ee8f-4e8b-9923-b8b41ced47cb Duration: 2495.95 ms, Billed Duration: 2496 ms, Memory Size: 512 MB, Max Memory Used: 85.32 MB ========= FC invoke Logs end ========= FC Invoke Result: ok End of method: invoke
相關文檔
如果您需要加快大視頻的轉碼速度或者完成多種複雜的組合操作,可以通過Serverless工作流程編排函數實現視頻處理系統,具體請參見構建基於Serverless架構的彈性高可用音視頻處理系統。
如果您需要即時或者准即時的音視頻處理,可以使用GPU執行個體部署對應的應用,具體請參見音視頻處理最佳實務。