author : Empty bad uncle
Blog :https://xuhss.com
The breakfast shop won't be open till night , The people who want to eat have already come !
XFFmpeg.h
Add member variables
Decoder context :AVCodecContext *vcodec
add to Close function
For release Context :
void Close();
The complete code is as follows
#pragma once
struct AVFormatContext;
struct AVPacket;
struct AVCodecContext;
class XFFmpeg
{
public:
// Open the video
bool Open(const char *url);
// Read a burst of video Store... Internally
bool Read();
void Close();
XFFmpeg();
~XFFmpeg();
int totalms = 0;
protected:
// Unpack context
AVFormatContext *ic = 0;
// Read video frames
AVPacket *pkt = 0;
// Decoder context
AVCodecContext *vcodec = 0;
};
XFFmpeg.cpp add to open and close Implementation of function
open Implementation of function
bool XFFmpeg::Open(const char *url)
{
printf("XFFmpeg::open %s\n", url);
// Open the video decapsulation
int re =avformat_open_input(&ic, url, 0, 0);
if (re != 0)
{
char buf[1024] = {
0 };
av_strerror(re, buf, 1023);
printf("avformat open fail:%s\n", buf);
return false;
}
// Get stream
avformat_find_stream_info(ic, 0);
// Total duration of video acquisition
this->totalms = ic->duration / (AV_TIME_BASE / 1000);
printf("Total Ms =%d\n", totalms);
// Turn on the decoder 0 video 1 Audio
AVCodecParameters *para = ic->streams[0]->codecpar;
// Find the decoder
AVCodec * codec = avcodec_find_decoder(para->codec_id);
if (!codec)
{
printf("avcodec_find_decoder %d failed!\n", para->codec_id);
}
// Assign decoder context
vcodec = avcodec_alloc_context3(codec);
// Configure decoder context
avcodec_parameters_to_context(vcodec, para);
// Multithreading decoding
vcodec->thread_count = 8;
// Open context
re = avcodec_open2(vcodec, 0, 0);
if (re != 0)
{
avcodec_free_context(&vcodec);
char buf[1024];
av_strerror(re, buf, sizeof(buf) - 1);
printf("avcodec_open2 failec:%s!\n", buf);
}
printf("avcodec_open2 successed \n");
return true;
}
Close Function implementation that :
void XFFmpeg::Close()
{
printf("XFFmpeg::Close\n");
if (ic)
{
avformat_close_input(&ic);
}
if (vcodec)
{
avcodec_free_context(&vcodec);
}
if (pkt)
{
av_packet_free(&pkt);
}
}
The complete code is as follows
#include "XFFmpeg.h"
#include <stdio.h>
extern "C" {
#include "libavformat\avformat.h"
}
void XFFmpeg::Close()
{
printf("XFFmpeg::Close\n");
if (ic)
{
avformat_close_input(&ic);
}
if (vcodec)
{
avcodec_free_context(&vcodec);
}
if (pkt)
{
av_packet_free(&pkt);
}
}
bool XFFmpeg::Open(const char *url)
{
printf("XFFmpeg::open %s\n", url);
// Open the video decapsulation
int re =avformat_open_input(&ic, url, 0, 0);
if (re != 0)
{
char buf[1024] = {
0 };
av_strerror(re, buf, 1023);
printf("avformat open fail:%s\n", buf);
return false;
}
// Get stream
avformat_find_stream_info(ic, 0);
// Total duration of video acquisition
this->totalms = ic->duration / (AV_TIME_BASE / 1000);
printf("Total Ms =%d\n", totalms);
// Turn on the decoder 0 video 1 Audio
AVCodecParameters *para = ic->streams[0]->codecpar;
// Find the decoder
AVCodec * codec = avcodec_find_decoder(para->codec_id);
if (!codec)
{
printf("avcodec_find_decoder %d failed!\n", para->codec_id);
}
// Assign decoder context
vcodec = avcodec_alloc_context3(codec);
// Configure decoder context
avcodec_parameters_to_context(vcodec, para);
// Multithreading decoding
vcodec->thread_count = 8;
// Open context
re = avcodec_open2(vcodec, 0, 0);
if (re != 0)
{
avcodec_free_context(&vcodec);
char buf[1024];
av_strerror(re, buf, sizeof(buf) - 1);
printf("avcodec_open2 failec:%s!\n", buf);
}
printf("avcodec_open2 successed \n");
return true;
}
bool XFFmpeg::Read()
{
if (!ic)
return false;
// Storage space of video frames
if (!pkt)
{
// Allocate object space
pkt = av_packet_alloc();
}
else
{
// Reference count -1 Clean up video frames
av_packet_unref(pkt);
}
int re = 0;
bool isFindVideo = false;
// Audio data lost
for (int i = 0; i < 20; i++)
{
// Read a frame of data
re = av_read_frame(ic, pkt);
// Read failed or read to the end of the file
if (re != 0)
{
return false;
}
// Is it a video frame
if (pkt->stream_index == 0)
{
isFindVideo = true;
break;
}
// Audio frame clear packet
av_packet_unref(pkt);
}
return isFindVideo;
}
XFFmpeg::XFFmpeg()
{
printf("Create XFFmpeg\n");
}
XFFmpeg::~XFFmpeg()
{
printf("Delete XFFmpeg\n");
}
PyFFmpeg.cpp Add Close Function call :
void PyFFmpeg::Close(PyFFmpeg*self)
{
printf("PyFFmpeg::Close\n");
self->ff->Close();
delete self->ff;
Py_TYPE(self)->tp_free(self);
}
After operation , You'll find that , Decoder context loaded successfully
give the thumbs-up
Collection
forward
A wave , Bloggers also support making exclusive dynamic wallpapers for iron fans ~Follow the card below to get more programming knowledge immediately , Including various language learning materials , Thousands of sets PPT Templates and various game source materials and so on . More information can be viewed by yourself !