两位小组成员对负责部分学习笔记如下:
图像转视频
工具:
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权限被拒绝
解决办法:
以管理员身份运行即可
视频转图像
av_register_all(); //初始化FFMPEG 调用了这个才能正常适用编码器和解码器
- 接着需要分配一个AVFormatContext,FFMPEG所有的操作都要通过这个AVFormatContext来进行
AVFormatContext *pFormatCtx = avformat_alloc_context();
- 接着调用打开视频文件
这里文件名先不要使用中文,否则会打开失败
char *file_path = "E:in.mp4";
avformat_open_input(&pFormatCtx, file_path, NULL, NULL);
- 查找文件中视频流
/*循环查找视频中包含的流信息,直到找到视频类型的流 便将其记录下来 保存到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)); //分配一个packet
av_new_packet(packet, y_size); //分配packet的数据
if (av_read_frame(pFormatCtx, packet) < 0)
{
break; //这里认为视频读取完了
}