兩位小組成員對(duì)負(fù)責(zé)部分學(xué)習(xí)筆記如下:
圖像轉(zhuǎn)視頻
工具:
FFmpeg命令行
具體命令行:
ffmpeg -f image2 -i C:\Users\Lenovo\Desktop\pict%d.jpg -vcodec libx264 -r 10 vid.mp4
命令行解析:
(1)-vcodec 選擇編碼libx264
(2)-r 幀率
(3)-i 圖片路徑
遇到的問題:
FFmpeg權(quán)限被拒絕
解決辦法:
以管理員身份運(yùn)行即可
視頻轉(zhuǎn)圖像
av_register_all(); //初始化FFMPEG 調(diào)用了這個(gè)才能正常適用編碼器和解碼器
- 接著需要分配一個(gè)AVFormatContext,F(xiàn)FMPEG所有的操作都要通過這個(gè)AVFormatContext來進(jìn)行
AVFormatContext *pFormatCtx = avformat_alloc_context();
- 接著調(diào)用打開視頻文件
這里文件名先不要使用中文,否則會(huì)打開失敗
char *file_path = "E:in.mp4";
avformat_open_input(&pFormatCtx, file_path, NULL, NULL);
- 查找文件中視頻流
/*循環(huán)查找視頻中包含的流信息,直到找到視頻類型的流 便將其記錄下來 保存到videoStream變量中*/
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
//如果videoStream為-1 說明沒有找到視頻流
if (videoStream == -1) {
printf("Didn't find a video stream.
");
return -1;
}
- 解碼視頻
//查找解碼器
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
printf("Codec not found.
");
return -1;
}
//打開解碼器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Could not open codec.
");
return -1;
}
- 讀取視頻
int y_size = pCodecCtx->width * pCodecCtx->height;
AVPacket *packet = (AVPacket *) malloc(sizeof(AVPacket)); //分配一個(gè)packet
av_new_packet(packet, y_size); //分配packet的數(shù)據(jù)
if (av_read_frame(pFormatCtx, packet) < 0)
{
break; //這里認(rèn)為視頻讀取完了
}