有奖捉虫:云通信与企业服务文档专题,速来> HOT

简介

本文档提供关于媒体信息接口的 API 概览和 SDK 示例代码。
API
操作名
操作描述
查询文件信息
用于查询媒体文件的信息
注意
使用此接口前,请确保已打开官网控制台中数据处理下的媒体处理开关,否则会报错media bucket unbinded, bucket's host is unavailable。详情请参见 开通媒体处理

查询文件信息

功能说明

用于获取媒体文件的信息。

方法原型

cos_status_t *ci_get_media_info(const cos_request_options_t *options,
const cos_string_t *bucket,
const cos_string_t *object,
cos_table_t *headers,
cos_table_t **resp_headers,
ci_media_info_result_t **media_result);

参数说明

参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
headers
COS 请求附加头域
Struct
resp_headers
返回 HTTP 响应消息的头域
Struct
media_result
返回媒体文件信息
Struct
format
格式信息
Struct
num_stream
Stream(包含 Video、Audio、Subtitle)的数量
Int
num_program
节目的数量
Int
format_name
容器格式名字
String
format_long_name
容器格式的详细名字
String
start_time
起始时间,单位为秒
Float
duration
时长,单位为秒
Float
bit_rate
比特率,单位为 kbps
Int
size
大小,单位为 Byte
Int
stream
流信息
Struct
video
视频信息
Struct
audio
音频信息
Struct
subtitle
字幕信息
Struct

video所对应的结构体说明

参数名称
参数描述
类型
index
该流的编号
Int
codec_name
编解码格式名字
String
codec_long_name
编解码格式的详细名字
String
codec_time_base
编码时基
String
codec_tag_string
编码标签名
String
codec_tag
编码标签
String
profile
视频编码档位
String
height
视频高,单位 px
Int
width
视频宽,单位 px
Int
has_b_frame
是否有B帧。1表示有,0表示无
Int
ref_frames
视频编码的参考帧个数
Int
sar
采样宽高比
String
dar
显示宽高比
String
pix_format
像素格式
String
field_order
场的顺序
String
level
视频编码等级
Int
fps
视频帧率
Int
avg_fps
平均帧率
String
timebase
时基
String
start_time
视频开始时间,单位为秒
Float
duration
视频时长,单位为秒
Float
bit_rate
比特率,单位为 kbps
Float
num_frames
总帧数
Int
language
语言
String

audio所对应的结构体说明

参数名称
参数描述
类型
index
该流的编号
Int
codec_name
编解码格式名字
String
codec_long_name
编解码格式的详细名字
String
codec_time_base
编码时基
String
codec_tag_string
编码标签名
String
codec_tag
编码标签
String
sample_fmt
采样格式
String
sample_rate
采样率
Int
channel
通道数量
Int
channel_layout
通道格式
String
timebase
时基
String
start_time
音频开始时间,单位为秒
Float
duration
音频时长,单位为秒
Float
bit_rate
比特率,单位为 kbps
Float
language
语言
String

subtitle所对应的结构体说明

参数名称
参数描述
类型
index
该流的编号
Int
language
语言,und 表示无查询结果
String

返回结果说明

返回结果
描述
类型
code
错误码
Int
error_code
错误码内容
String
error_msg
错误码描述
String
req_id
请求消息 ID
String

示例

完整代码请参见cos_demo.c中test_ci_media_process_media_info()函数。
#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"
#include <sys/stat.h>

// endpoint 是 COS 访问域名信息,详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";

void log_status(cos_status_t *s)
{
cos_warn_log("status->code: %d", s->code);
if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);
}

static void log_media_info_result(ci_media_info_result_t *result)
{
// format
cos_warn_log("format.num_stream: %d", result->format.num_stream);
cos_warn_log("format.num_program: %d", result->format.num_program);
cos_warn_log("format.format_name: %s", result->format.format_name.data);
cos_warn_log("format.format_long_name: %s", result->format.format_long_name.data);
cos_warn_log("format.start_time: %f", result->format.start_time);
cos_warn_log("format.duration: %f", result->format.duration);
cos_warn_log("format.bit_rate: %d", result->format.bit_rate);
cos_warn_log("format.size: %d", result->format.size);

// stream.video
cos_warn_log("stream.video.index: %d", result->stream.video.index);
cos_warn_log("stream.video.codec_name: %s", result->stream.video.codec_name.data);
cos_warn_log("stream.video.codec_long_name: %s", result->stream.video.codec_long_name.data);
cos_warn_log("stream.video.codec_time_base: %s", result->stream.video.codec_time_base.data);
cos_warn_log("stream.video.codec_tag_string: %s", result->stream.video.codec_tag_string.data);
cos_warn_log("stream.video.codec_tag: %s", result->stream.video.codec_tag.data);
cos_warn_log("stream.video.profile: %s", result->stream.video.profile.data);
cos_warn_log("stream.video.height: %d", result->stream.video.height);
cos_warn_log("stream.video.width: %d", result->stream.video.width);
cos_warn_log("stream.video.has_b_frame: %d", result->stream.video.has_b_frame);
cos_warn_log("stream.video.ref_frames: %d", result->stream.video.ref_frames);
cos_warn_log("stream.video.sar: %s", result->stream.video.sar.data);
cos_warn_log("stream.video.dar: %s", result->stream.video.dar.data);
cos_warn_log("stream.video.pix_format: %s", result->stream.video.pix_format.data);
cos_warn_log("stream.video.field_order: %s", result->stream.video.field_order.data);
cos_warn_log("stream.video.level: %d", result->stream.video.level);
cos_warn_log("stream.video.fps: %d", result->stream.video.fps);
cos_warn_log("stream.video.avg_fps: %s", result->stream.video.avg_fps.data);
cos_warn_log("stream.video.timebase: %s", result->stream.video.timebase.data);
cos_warn_log("stream.video.start_time: %f", result->stream.video.start_time);
cos_warn_log("stream.video.duration: %f", result->stream.video.duration);
cos_warn_log("stream.video.bit_rate: %f", result->stream.video.bit_rate);
cos_warn_log("stream.video.num_frames: %d", result->stream.video.num_frames);
cos_warn_log("stream.video.language: %s", result->stream.video.language.data);

// stream.audio
cos_warn_log("stream.audio.index: %d", result->stream.audio.index);
cos_warn_log("stream.audio.codec_name: %s", result->stream.audio.codec_name.data);
cos_warn_log("stream.audio.codec_long_name: %s", result->stream.audio.codec_long_name.data);
cos_warn_log("stream.audio.codec_time_base: %s", result->stream.audio.codec_time_base.data);
cos_warn_log("stream.audio.codec_tag_string: %s", result->stream.audio.codec_tag_string.data);
cos_warn_log("stream.audio.codec_tag: %s", result->stream.audio.codec_tag.data);
cos_warn_log("stream.audio.sample_fmt: %s", result->stream.audio.sample_fmt.data);
cos_warn_log("stream.audio.sample_rate: %d", result->stream.audio.sample_rate);
cos_warn_log("stream.audio.channel: %d", result->stream.audio.channel);
cos_warn_log("stream.audio.channel_layout: %s", result->stream.audio.channel_layout.data);
cos_warn_log("stream.audio.timebase: %s", result->stream.audio.timebase.data);
cos_warn_log("stream.audio.start_time: %f", result->stream.audio.start_time);
cos_warn_log("stream.audio.duration: %f", result->stream.audio.duration);
cos_warn_log("stream.audio.bit_rate: %f", result->stream.audio.bit_rate);
cos_warn_log("stream.audio.language: %s", result->stream.audio.language.data);

// stream.subtitle
cos_warn_log("stream.subtitle.index: %d", result->stream.subtitle.index);
cos_warn_log("stream.subtitle.language: %s", result->stream.subtitle.language.data);
}

void test_ci_media_process_media_info()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers;
ci_media_info_result_t *media_info;
cos_string_t object;

// 基本配置
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
options->config = cos_config_create(options->pool);
cos_str_set(&options->config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&options->config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&options->config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&options->config->appid, TEST_APPID);
options->config->is_cname = is_cname;
options->ctl = cos_http_controller_create(options->pool, 0);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, "test.mp4");

// 替换为您的配置信息,可参见文档 https://cloud.tencent.com/document/product/436/55672
s = ci_get_media_info(options, &bucket, &object, NULL, &resp_headers, &media_info);
log_status(s);
if (s->code == 200) {
log_media_info_result(media_info);
}

// 销毁内存池
cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// 通过环境变量获取 SECRETID 和 SECRETKEY
TEST_ACCESS_KEY_ID = getenv("COS_SECRETID");
TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");

if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}

//set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_ci_media_process_media_info();

cos_http_io_deinitialize();

return 0;
}