作者:虛壞叔叔
博客:https://xuhss.com
早餐店不會開到晚上,想吃的人早就來了!
通過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;
}
運行:
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;
}
testmod.py添加對擴展庫接口的調用
# import pyffmpeg
from pyffmpeg import *
ff = PyFFmpeg()
ff.open("video.mp4")
print("in Python totalms = ", ff.totalms)
del ff
input()
可以看到成功在python中讀取到。
本文完成python調用擴展庫實現視頻信息讀取 。
如果覺得文章對你有用處,記得 點贊
收藏
轉發
一波哦,博主也支持為鐵粉絲制作專屬動態壁紙哦~
關注下面卡片即刻獲取更多編程知識,包括各種語言學習資料,上千套PPT模板和各種游戲源碼素材等等資料。更多內容可自行查看哦!