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 !
PyFFmpeg.h
add to :
static PyObject* Open(PyFFmpeg*self, PyObject*args);
PyFFmpeg.cpp
add to :
#include "PyFFmpeg.h"
// Open to python
PyObject *PyFFmpeg::Create(PyTypeObject *type, PyObject *args, PyObject *kw) {
printf("PyFFmpeg::Create\n");
return type->tp_alloc(type, 0);
}
int PyFFmpeg::Init(PyFFmpeg*self, PyObject *args, PyObject *kw)
{
printf("PyFFmpeg::Init\n");
return 0;
}
void PyFFmpeg::Close(PyFFmpeg*self)
{
printf("PyFFmpeg::Close\n");
Py_TYPE(self)->tp_free(self);
}
PyObject* PyFFmpeg::Open(PyFFmpeg*self, PyObject*args)
{
const char *url = NULL;
if (!PyArg_ParseTuple(atgs, "s", &url))
return NULL;
printf("PyFFmpeg::Open %s\n", url);
Py_RETURN_TRUE;
}
// Module entry Module name pyffmpeg
PyMODINIT_FUNC PyInit_pyffmpeg(void)
{
PyObject *m = NULL;
static PyModuleDef ffmod = {
PyModuleDef_HEAD_INIT,
"pyffmpeg",
"", -1, 0
};
m = PyModule_Create(&ffmod);
// add to PyFFmpeg_python class
static PyTypeObject type;
memset(&type, 0, sizeof(PyFFmpeg));
type.ob_base = {
PyObject_HEAD_INIT(NULL) 0 };
type.tp_name = "";
type.tp_basicsize = sizeof(PyFFmpeg);
type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
type.tp_new = PyFFmpeg::Create;
type.tp_init = (initproc)PyFFmpeg::Init;
type.tp_dealloc = (destructor)PyFFmpeg::Close;
static PyMethodDef ffmeth[] = {
{
"open", (PyCFunction)PyFFmpeg::Open, METH_VARARGS, ""},
{
NULL}
}
type.tp_methods = ffmeth;
// Initialization type
if (PyType_Ready(&type) < 0) {
return NULL;
}
PyModule_AddObject(m, "PyFFmpeg", (PyObject*)&type);
printf("Pyinit_pyffmpeg\n");
return m;
}
testmod.py Add call :
ff = PyFFmpeg()
ff.open("video.mp4")
del ff
function :
Add the class XFFmpeg
XFFmpeg.h
The code is as follows :
#pragma once
class XFFmpeg
{
public:
bool Open(const char *url);
XFFmpeg();
~XFFmpeg();
};
XFFmpeg.cpp
The code is as follows :
#include "XFFmpeg.h"
#include <stdio.h>
bool XFFmpeg::Open(const char *url)
{
printf("XFFmpeg::open %s\n", url);
return true;
}
XFFmpeg::XFFmpeg()
{
printf("Create XFFmpeg\n");
}
XFFmpeg::~XFFmpeg()
{
printf("Delete XFFmpeg\n");
}
PyFFmpeg.h
The code is as follows :
#pragma once
#include<Python.h>
class XFFmpeg;
class PyFFmpeg
{
public:
PyObject_HEAD
XFFmpeg *ff;
// Open to python Function of
public:
static PyObject *Create(PyTypeObject *type, PyObject *args, PyObject *kw);
static int Init(PyFFmpeg*self, PyObject *args, PyObject *kw);
static void Close(PyFFmpeg*self);
static PyObject* Open(PyFFmpeg*self, PyObject*args);
};
PyFFmpeg.cpp
The code is as follows :
#include "PyFFmpeg.h"
#include "XFFmpeg.h"
// Open to python
PyObject *PyFFmpeg::Create(PyTypeObject *type, PyObject *args, PyObject *kw) {
printf("PyFFmpeg::Create\n");
PyFFmpeg*f = (PyFFmpeg*)type->tp_alloc(type, 0);
f->ff = new XFFmpeg();
return (PyObject *)f;
}
int PyFFmpeg::Init(PyFFmpeg*self, PyObject *args, PyObject *kw)
{
printf("PyFFmpeg::Init\n");
return 0;
}
void PyFFmpeg::Close(PyFFmpeg*self)
{
printf("PyFFmpeg::Close\n");
delete self->ff;
Py_TYPE(self)->tp_free(self);
}
PyObject* PyFFmpeg::Open(PyFFmpeg*self, PyObject*args)
{
const char *url = NULL;
if (!PyArg_ParseTuple(args, "s", &url))
return NULL;
printf("PyFFmpeg::Open %s\n", url);
if (self->ff->Open(url))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
// Module entry Module name pyffmpeg
PyMODINIT_FUNC PyInit_pyffmpeg(void)
{
PyObject *m = NULL;
static PyModuleDef ffmod = {
PyModuleDef_HEAD_INIT,
"pyffmpeg",
"", -1, 0
};
m = PyModule_Create(&ffmod);
// add to PyFFmpeg_python class
static PyTypeObject type;
memset(&type, 0, sizeof(PyFFmpeg));
type.ob_base = {
PyObject_HEAD_INIT(NULL) 0 };
type.tp_name = "";
type.tp_basicsize = sizeof(PyFFmpeg);
type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
type.tp_new = PyFFmpeg::Create;
type.tp_init = (initproc)PyFFmpeg::Init;
type.tp_dealloc = (destructor)PyFFmpeg::Close;
static PyMethodDef ffmeth[] = {
{
"open", (PyCFunction)PyFFmpeg::Open, METH_VARARGS, "" },
{
NULL }
};
type.tp_methods = ffmeth;
// Initialization type
if (PyType_Ready(&type) < 0) {
return NULL;
}
PyModule_AddObject(m, "PyFFmpeg", (PyObject*)&type);
printf("Pyinit_pyffmpeg\n");
return m;
}
Download the file for class
Copy include
、 lib
Catalog :
lib Library chairs only copy 32 position :
Direct copy dll To project directory :
Additional include directory add .\include
Additional Library Directory :
Add additional dependent libraries avformat.lib
avformat.lib
avutil.lib
avcodec.lib
Open the video decapsulation
xxFFmpeg.h as follows :
#pragma once
struct AVFormatContext;
class XFFmpeg
{
public:
bool Open(const char *url);
XFFmpeg();
~XFFmpeg();
protected:
AVFormatContext *ic = 0;
};
xFFmpeg.cpp as follows
#include "XFFmpeg.h"
#include <stdio.h>
extern "C" {
#include "libavformat\avformat.h"
}
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", buf);
return false;
}
return true;
}
XFFmpeg::XFFmpeg()
{
printf("Create XFFmpeg\n");
}
XFFmpeg::~XFFmpeg()
{
printf("Delete XFFmpeg\n");
}
Compiler error :
Need to adjust : senior -》 The image has a secure exception handler
function :
Prompt no files
take video.mp4
Put it in , The next lesson will be played normally .
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 !