我尝试使用提供h264原始压缩数据(附件b格式)的相机创建mp4视频,无需解码或编码操作。我使用v4l2从相机获取帧,并为每个帧填充AVPacket并使用av_write_frame将其写入文件。它运行良好,但视频不是那么流畅,使用ffprobe我注意到视频中没有B帧,只有I帧和P帧。
for(int i = 0; i<100; i++){
// Dequeue the buffer
if(ioctl(fd, VIDIOC_DQBUF, &bufferinfo) < 0)
perror("Could not dequeue the buffer, VIDIOC_DQBUF");
AVPacket packet;
av_init_packet(&packet);
packet.stream_index = videoStream->index;
//buffer is allocated with mmap
packet.data = (uint8_t*)buffer;
//byteused is about 20/40 kB
packet.size = bufferinfo.bytesused;
packet.pts = (1.0 / 30) * 24000 * i;
err = av_write_frame(outputFormatCtx, &packet);
if(err < 0)
cout << "Error write frame "<<endl;
printf("Write frame (size= %2d)\n", packet.size);
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0){
perror("Could not queue buffer, VIDIOC_QBUF");
return 1;
}
av_packet_unref(&packet);
}
err = av_write_trailer(outputFormatCtx);
if( err < 0)
cout<<"Error : av_write_trailer()" << endl;
我不明白问题是捕获还是文件写入。 感谢。