天天看点

Python&C++相互混合调用编程全面实战-28完成python调用扩展库实现视频信息读取

作者:虚坏叔叔

早餐店不会开到晚上,想吃的人早就来了!😄

完成python调用扩展库实现视频信息读取

Python&C++相互混合调用编程全面实战-28完成python调用扩展库实现视频信息读取

一、读取视频总时长

通过ffmpeg读取视频总时长

bool XFFmpeg::Open(const char *url)
{
  printf("XFFmpeg::open %s\n", url);
  // 打开视频 解封装
  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;
  }

  // 获取流
  avformat_find_stream_info(ic, 0);

  // 获取视频总时长
  int totalms =  ic->duration / (AV_TIME_BASE / 1000);
  printf("Total Ms =%d\n", totalms);


  return true;
}      

运行:

Python&C++相互混合调用编程全面实战-28完成python调用扩展库实现视频信息读取

二、将读取时长接口开放给python

​XFFmpeg.h​

​​添加成员变量​

​totalms​

#pragma once
struct AVFormatContext;
class XFFmpeg
{
public:
  bool Open(const char *url);
  XFFmpeg();
  ~XFFmpeg();
  int totalms = 0;
protected:
  AVFormatContext *ic = 0;
};      

​XFFmpeg.cpp​

​添加设置成员变量

#include "XFFmpeg.h"
#include <stdio.h>
extern "C" {
#include "libavformat\avformat.h"
}

bool XFFmpeg::Open(const char *url)
{
  printf("XFFmpeg::open %s\n", url);
  // 打开视频 解封装
  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;
  }

  // 获取流
  avformat_find_stream_info(ic, 0);

  // 获取视频总时长
  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添加GetTotalms函数

#pragma once
#include<Python.h>
class XFFmpeg;
class PyFFmpeg
{
public:
  PyObject_HEAD

  XFFmpeg *ff;

//开放给python的函数
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);
  // 属性函数 get
  static PyObject* GetTotalms(PyFFmpeg*self, void*closure);

};      

​PyFFmpeg.cpp​

​添加实现和注册

#include "PyFFmpeg.h"
#include "XFFmpeg.h"
// 开放给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);

}

// 模块入口 模块名称 pyffmpeg
PyMODINIT_FUNC PyInit_pyffmpeg(void)
{
  PyObject *m = NULL;
  static PyModuleDef ffmod = {
    PyModuleDef_HEAD_INIT,
    "pyffmpeg",
    "", -1, 0
  };
  m = PyModule_Create(&ffmod);

  // 添加PyFFmpeg_python类
  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;

  // 初始化类型
  if (PyType_Ready(&type) < 0) {
    return NULL;
  }

  PyModule_AddObject(m, "PyFFmpeg", (PyObject*)&type);

  printf("Pyinit_pyffmpeg\n");
  return m;
}      
# import pyffmpeg
from pyffmpeg import *
ff = PyFFmpeg()
ff.open("video.mp4")
print("in Python totalms = ", ff.totalms)
del ff
input()      

三、总结

  • 本文完成python调用扩展库实现视频信息读取 。
  • 如果觉得文章对你有用处,记得​

    ​点赞​

    ​ ​

    ​收藏​

    ​ ​

    ​转发​

    ​ 一波哦,博主也支持为铁粉丝制作专属动态壁纸哦~