程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Comprehensive practice of Python & c++ mixed call programming -28 complete Python call extension library to read video information

編輯:Python

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 !

complete python Call the extension library to read video information

One 、 Total time of reading video

adopt ffmpeg Total time of reading video

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 
int totalms = ic->duration / (AV_TIME_BASE / 1000);
printf("Total Ms =%d\n", totalms);
return true;
}

function :

Two 、 Open the read duration interface to python

XFFmpeg.h Add member variables totalms

#pragma once
struct AVFormatContext;
class XFFmpeg
{

public:
bool Open(const char *url);
XFFmpeg();
~XFFmpeg();
int totalms = 0;
protected:
AVFormatContext *ic = 0;
};

XFFmpeg.cpp Add setting member variables

#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:%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);
return true;
}
XFFmpeg::XFFmpeg()
{

printf("Create XFFmpeg\n");
}
XFFmpeg::~XFFmpeg()
{

printf("Delete XFFmpeg\n");
}

PyFFmpeg.h add to GetTotalms function

#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);
// Property function get
static PyObject* GetTotalms(PyFFmpeg*self, void*closure);
};

PyFFmpeg.cpp Add implementation and registration

#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;
}
PyObject* PyFFmpeg::GetTotalms(PyFFmpeg*self, void*closure) {

return PyLong_FromLong(self->ff->totalms);
}
// 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;
static PyGetSetDef sets[] = {

{
 "totalms", (getter)PyFFmpeg::GetTotalms, 0, 0,0 },
{
 NULL }
};
type.tp_getset = sets;
// Initialization type 
if (PyType_Ready(&type) < 0) {

return NULL;
}
PyModule_AddObject(m, "PyFFmpeg", (PyObject*)&type);
printf("Pyinit_pyffmpeg\n");
return m;
}

testmod.py Add a call to the extension library interface

# import pyffmpeg
from pyffmpeg import *
ff = PyFFmpeg()
ff.open("video.mp4")
print("in Python totalms = ", ff.totalms)
del ff
input()

You can see success in python Read in the .

3、 ... and 、 summary

  • In this paper, complete python Call the extension library to read video information .

  • If you think the article is useful to you , Remember give the thumbs-up Collection forward A wave , Bloggers also support making exclusive dynamic wallpapers for iron fans ~

Share high-quality articles in previous periods

  • C++ QT combination FFmpeg Actual development of video player -01 Environment installation and project deployment
  • solve QT problem : function qmake:Project ERROR: Cannot run compiler ‘cl‘. Output:
  • Resolve installation QT after MSVC2015 64bit No compiler and debugger issues with configuration
  • Qt Kit tips in no complier set in kit and no debugger, The yellow exclamation mark appears and the problem is solved (MSVC2017)
  • Python+selenium automation - Realize automatic import 、 Upload external files ( Don't pop up windows window )

High quality tutorial sharing

  • If you don't enjoy reading the article , You can come to my other special column Take a look ~
  • For example, the following columns :Python Actual wechat ordering applet 、Python Quantitative trading practice 、C++ QT Practical projects and Algorithm learning column
  • You can learn more about C++/Python Relevant contents of ! Directly click on the color font below to jump !
Learning route guidance ( Click unlock ) Knowledge orientation Crowd positioning 🧡 Python Actual wechat ordering applet 🧡 Progressive class This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system .Python Quantitative trading practice beginner Take you hand in hand to create an easy to expand 、 More secure 、 More efficient Quantitative trading System ️ C++ QT combination FFmpeg Actual development of video player ️ The difficulty is high Sharing learning QT Finished video player source code , We need to have a solid C++ knowledge ! A community of 90000 game lovers Help each other / Blow water 90000 game lovers community , Chat and help each other , White whoring prize Python Zero basis to introduction Python beginner For small partners who have not been systematically studied , The core purpose is to enable us to learn quickly Python Knowledge to get started

Data white whoring , reminder

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 !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved