我正在尝试构建一个使用 ffmpeg 库的基于 C 的 Python 扩展。由于有一堆库我必须导入。我使用了 CMake。我按照python doc的说明进行操作
使用 CMake 构建运行良好,没有任何问题。但是当我导入库时,我得到以下异常。
Fatal Python error: PyThreadState_Get: no current thread
我正在使用带有 python2.7.14 的 mac(使用 Brew 安装)。
以下是我的
CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(extractor C)
set(CMAKE_C_STANDARD 99)
set(EXECUTABLE_NAME "extractor")
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)
set(EXTRACTOR_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
set(target_dir "bin")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -std=99")
add_library(${EXECUTABLE_NAME} SHARED src/frame_extractor.c)
# add_library(${EXECUTABLE_NAME} SHARED src/dipoza.c)
# add_executable(${EXECUTABLE_NAME} main.c)
include_directories(".")
find_package(FFmpeg REQUIRED)
if (FFMPEG_FOUND)
message("Found FFMPEG/LibAV libraries ${FFMPEG_LIBRARIES}")
include_directories(${FFMPEG_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${FFMPEG_LIBRARIES})
else (FFMPEG_FOUND)
message("cant find libavcodec, libavformat or libavutil. add them manually!!")
endif (FFMPEG_FOUND)
target_link_libraries(${EXECUTABLE_NAME} ${FFMPEG_LIBRARIES})
target_link_libraries (${EXECUTABLE_NAME} "-lm")
target_link_libraries(${EXECUTABLE_NAME} "-framework CoreServices")
target_link_libraries(${EXECUTABLE_NAME} "-framework CoreFoundation")
target_link_libraries(${EXECUTABLE_NAME} "-framework AudioToolbox")
target_link_libraries(${EXECUTABLE_NAME} "-framework VideoToolbox")
target_link_libraries(${EXECUTABLE_NAME} "-framework CoreAudio")
target_link_libraries(${EXECUTABLE_NAME} "-framework CoreMedia")
target_link_libraries(${EXECUTABLE_NAME} "-framework CoreVideo")
target_link_libraries(${EXECUTABLE_NAME} "-framework AVFoundation")
target_link_libraries(${EXECUTABLE_NAME} "-framework Security")
target_link_libraries(${EXECUTABLE_NAME} /usr/lib/libbz2.1.0.dylib)
target_link_libraries(${EXECUTABLE_NAME} /usr/lib/libz.1.dylib)
target_link_libraries(${EXECUTABLE_NAME} /usr/lib/libiconv.dylib)
target_link_libraries(${EXECUTABLE_NAME} /usr/lib/liblzma.dylib)
target_link_libraries(${EXECUTABLE_NAME} /usr/local/lib/libswresample.a)
target_link_libraries(${EXECUTABLE_NAME} /usr/local/Cellar/fdk-aac/0.1.6/lib/libfdk-aac.dylib)
target_link_libraries(${EXECUTABLE_NAME} /usr/local/lib/libx264.dylib)
# Add python dependencies -
# ERROR - It picks up the default version from the machine which is 2.7.10
# find_package( PythonInterp 2.7 REQUIRED)
# find_package(PythonLibs 2.7 REQUIRED)
message("GURU - python include dirs = ${PYTHON_INCLUDE_DIRS}")
message("GURU - python include dirs = ${PYTHON_LIBRARIES}")
set(PYTHON_INCLUDE_DIRS "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7")
set(PYTHON_LIBRARIES "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib")
message("GURU - python include dirs = ${PYTHON_INCLUDE_DIRS}")
message("GURU - python include dirs = ${PYTHON_LIBRARIES}")
# dipoza()
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(${EXECUTABLE_NAME} ${PYTHON_LIBRARIES})
# Apple creates annoying dylib extension for shared libraries
if(APPLE)
SET(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif(APPLE)
即使是像下面这样的简单程序也会失败
static PyObject* extractor(PyObject* self, PyObject* args, PyObject* keywds) {
int width=1280, height=720;
char *file_path = NULL;
static char *kwlist[] = {"width", "height", "file_path", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii|s", kwlist, &width, &height, &file_path)) {
return NULL;
}
printf("input parameters width=%d height=%d file_name=%s", width, height, file_path);
return NULL;
}
static char extractor_docs[] =
"add( ): add all elements of the list\n";
static PyMethodDef extractor_methods[] = {
{"extractor", (PyCFunction)extractor, METH_VARARGS|METH_KEYWORDS, extractor_docs },
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initextractor(void) {
Py_InitModule3("extractor", extractor_methods, "Add all ze methods");
}
非常感谢您为解决此问题提供的任何帮助。