作者:虛壞叔叔
博客:https://xuhss.com
早餐店不會開到晚上,想吃的人早就來了!
XFFmpeg.h
添加讀取視頻幀函數
#pragma once
struct AVFormatContext;
struct AVPacket;
class XFFmpeg
{
public:
// 打開視頻
bool Open(const char *url);
// 讀取一陣視頻 在內部存儲
bool Read();
XFFmpeg();
~XFFmpeg();
int totalms = 0;
protected:
// 解封裝上下文
AVFormatContext *ic = 0;
// 讀取視頻幀
AVPacket *pkt = 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;
}
bool XFFmpeg::Read()
{
if (!ic)
return false;
// 視頻幀的存儲空間
if (!pkt)
{
// 分配對象空間
pkt = av_packet_alloc();
}
else
{
// 引用計數 -1 清理視頻幀
av_packet_unref(pkt);
}
int re = 0;
bool isFindVideo = false;
// 音頻數據丟掉
for (int i = 0; i < 20; i++)
{
// 讀取一幀數據
re = av_read_frame(ic, pkt);
// 讀取失敗或者讀取到文件結尾
if (re != 0)
{
return false;
}
// 是否是視頻幀
if (pkt->stream_index == 0)
{
isFindVideo = true;
break;
}
// 音頻幀 清理packet
av_packet_unref(pkt);
}
return isFindVideo;
}
XFFmpeg::XFFmpeg()
{
printf("Create XFFmpeg\n");
}
XFFmpeg::~XFFmpeg()
{
printf("Delete XFFmpeg\n");
}
PyFFmpeg.h
開放接口,方便python調用
#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);
static PyObject *Read(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::Read(PyFFmpeg*self, PyObject*args)
{
if (!self->ff)
Py_RETURN_FALSE;
if (self->ff->Read())
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
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, "" },
{
"read", (PyCFunction)PyFFmpeg::Read, METH_NOARGS, "" },
{
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;
}
pyqt.py
添加對接口的調用
isRunning = True
#主函數 在子線程中調用,線程是c++創建
def main():
print("Python main")
global ff;
while isRunning:
print(ff.read());
pyplayer.cpp
構造函數中調用main
函數調用 這裡不開啟線程
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
// 載入模塊
pModule = PyImport_ImportModule("pyqt");
if (!pModule)
{
printf("PyImport import error");
PyErr_Print();
return;
}
// 獲取python配置項 改變窗口的大小和標題
PyObject *conf = PyObject_GetAttrString(pModule, "conf");
if (!conf)
{
cout << "Please set the conf" << endl;
PyErr_Print();
return;
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
// 改變窗口標題
key = PyUnicode_FromString("title");
wchar_t title[1024] = {
0 };
PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
this->setWindowTitle(QString::fromUtf16((char16_t*)title));
Py_XDECREF(key);
if (width > 0 && height > 0)
{
resize(width, height);
}
Py_XDECREF(conf);
// 開放選擇文件的接口給python
static PyMethodDef meths[] = {
{
"OpenDialog", (PyCFunction)OpenDialog, METH_NOARGS, 0 }
,{
NULL }
};
int re = PyModule_AddFunctions(pModule, meths);
if (!re)
{
PyErr_Print();
}
// 開啟線程 調用python的 main函數
PyObject*main_fun = PyObject_GetAttrString(pModule, "main");
if (!main_fun || !PyCallable_Check(main_fun))
{
cout << "main_fun get failed!" << endl;
return;
}
if (PyObject_CallObject(main_fun, 0))
{
PyErr_Print();
return;
}
}
運行,你會發現一直輸出false
,並且主線程卡死,因為是在一個線程中,python
的main
函數有一個循環,不會退出的循環。
添加一個線程函數run_main
,並將執行main
函數的代碼放到這個線程中
#include <Python.h>
#include "PyPlayer.h"
#include <iostream>
#include <QFileDialog>
#include <thread>
using namespace std;
static PyObject *pModule = 0;
// 返回選擇的文件路徑
PyObject* OpenDialog(PyObject *self)
{
QString fileName = "";
fileName = QFileDialog::getOpenFileName();
if (fileName.isEmpty())
return PyUnicode_FromString("");
return PyUnicode_FromString(fileName.toStdString().c_str());
}
void PyPlayer::Open() {
cout << "PyPlayer::Open()" << endl;
// 調用Python的open函數
if (!pModule) return;
PyObject *open = PyObject_GetAttrString(pModule, "open");
if (!open || !PyCallable_Check(open))
{
PyErr_Print();
return;
}
PyObject_CallObject(open, 0);
}
void run_main()
{
PyObject*main_fun = PyObject_GetAttrString(pModule, "main");
if (!main_fun || !PyCallable_Check(main_fun))
{
cout << "main_fun get failed!" << endl;
return;
}
if (PyObject_CallObject(main_fun, 0))
{
PyErr_Print();
return;
}
}
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
// 載入模塊
pModule = PyImport_ImportModule("pyqt");
if (!pModule)
{
printf("PyImport import error");
PyErr_Print();
return;
}
// 獲取python配置項 改變窗口的大小和標題
PyObject *conf = PyObject_GetAttrString(pModule, "conf");
if (!conf)
{
cout << "Please set the conf" << endl;
PyErr_Print();
return;
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
// 改變窗口標題
key = PyUnicode_FromString("title");
wchar_t title[1024] = {
0 };
PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
this->setWindowTitle(QString::fromUtf16((char16_t*)title));
Py_XDECREF(key);
if (width > 0 && height > 0)
{
resize(width, height);
}
Py_XDECREF(conf);
// 開放選擇文件的接口給python
static PyMethodDef meths[] = {
{
"OpenDialog", (PyCFunction)OpenDialog, METH_NOARGS, 0 }
,{
NULL }
};
int re = PyModule_AddFunctions(pModule, meths);
if (!re)
{
PyErr_Print();
}
// 開啟線程 調用python的 main函數
std::thread t1(run_main);
t1.detach();
}
在執行,界面出來了:
點擊打開按鈕 發現程序崩潰了:
在PyPlayer.cpp
中open
函數中添加gil
鎖
void PyPlayer::Open() {
cout << "PyPlayer::Open()" << endl;
// 調用Python的open函數
if (!pModule) return;
PyGILState_STATE gil;
gil = PyGILState_Ensure();
PyObject *open = PyObject_GetAttrString(pModule, "open");
if (!open || !PyCallable_Check(open))
{
PyErr_Print();
PyGILState_Release(gil);
return;
}
PyObject_CallObject(open, 0);
PyGILState_Release(gil);
}
再次運行就不會崩潰了。
修改pyqt.py
將讀取視頻的過程通過一個點一個點的輸出出來,這樣就可以驗證讀取的線程是沒有問題的
print("Python PyPlayer")
from pyffmpeg import *
import time
conf = {
"width" : 1280,
"height" : 720,
"title" : "PyPlayer播放器"
}
ff = PyFFmpeg()
def open():
global ff;
print("Python open")
filename = OpenDialog()
if ff.open(filename):
print("open file success!")
else:
print("open file failed!")
print(filename)
isRunning = True
#主函數 在子線程中調用,線程是c++創建
def main():
print("Python main")
global ff;
while isRunning:
#print(ff.read())
re = ff.read()
if re:
print(".", end='', flush = True) #flush輸出緩沖
time.sleep(0.02)
else:
time.sleep(1)
點贊
收藏
轉發
一波哦,博主也支持為鐵粉絲制作專屬動態壁紙哦~關注下面卡片即刻獲取更多編程知識,包括各種語言學習資料,上千套PPT模板和各種游戲源碼素材等等資料。更多內容可自行查看哦!