windows 下编译 python_rtmpstream

最近在研究数字人,看了大咖的项目(
https://github.com/lipku/metahuman-stream),尝试编译此项目的依赖项目python_rtmpstream(
https://github.com/lipku/python_rtmpstream)。由于python_rtmpstream主要针对linux下,没有Windows的版本,因此编译出现较多的波折,但最终编译成功,特此记录。

一、编译环境说明

1、OS:Windows 10 专业版, 64 位操作系统, 基于 x64 的处理器

2、conda:安装的是
Anaconda3-2024.02-1-Windows-x86_64.exe

3、C++ Build tool:下载的是 vs_BuildTools.exe,安装后在C:\Program Files (x86)\Microsoft Visual Studio\2022,安装完成后设置环境变量 Path,增加如下两个路径:

(1)C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64

(2)C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin。这个是setup.py里需要cmake来编译C++代码,如果不设置,会报:error: [WinError 2] 系统找不到指定的文件。

4、SRS:流媒体服务器,安装的是
SRS-Windows-x86_64-5.0-r2-setup.exe,在运行 test_stream.py需要运行流媒体服务器。

二、建立项目环境

conda create -n nerfstream python=3.10

conda activate nerfstream

注意要做 cmd 下使用conda, 不要在 powershell 下使用

三、安装项目依赖

1、下载代码

git clone https://github.com/lipku/python_rtmpstream
cd python_rtmpstream 
git submodule update --init

2、安装依赖

pip install wheel
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda install ffmpeg 

四、修改CMakeLists.txt

注意:路径改为自己 conda nerfstream环境的路径,如果路径不正确,会报The following variables are used in this project, but they are set to NOTFOUND

五、修改streamer.cpp 和 streamer.hpp

1、streamer.cpp

解决如下三个问题

unistd.h、sys\time.h 文件找不到文件

gettimeofday在Windows下不适用

后跟初始值设定项列表的带圆括号类型是一个非标准的显式类型转换语法

(1)头文件增加如下,以便适配Windows环境

原来是

#include 
#include 
#include 
#include 
#include 
#include <sys/time.h>

改为

#include 
#include 
#include 
#include 
#ifdef _WIN32
#include 
#include 
#include 
#else
#include 
#include <sys/time.h>
#endif

(2)修改timems 函数

原来

static int64_t timems()
{
		struct timespec now;

		clock_gettime(CLOCK_BOOTTIME,&now);

		return (now.tv_sec*1000 + now.tv_nsec/1000/1000);
}

改为

static int64_t timems()
{
	#ifdef _WIN32
		// Windows 平台特定实现
		struct timespec ts;

		if (timespec_get(&ts, TIME_UTC) != TIME_UTC)
		{
			fputs("timespec_get failed!", stderr);
			return 0;
		}
		return (ts.tv_sec*1000 + ts.tv_nsec/1000/1000);
	#else
		// POSIX 平台特定实现
		struct timespec now;

		clock_gettime(CLOCK_BOOTTIME,&now);

		return (now.tv_sec*1000 + now.tv_nsec/1000/1000);
	#endif	
}

3、后跟初始值设定项列表的带圆括号类型是一个非标准的显式类型转换语法错误

codec_ctx->time_base = (AVRational){ 1, samplerate };
改为
codec_ctx->time_base = AVRational{ 1, samplerate };

2、streamer.hpp

解决posix_memalign 标识符问题

(1)头文件增加 #include

(2)修改posix_memalign

将 int ret = posix_memalign(reinterpret_cast(&data), align_frame_buffer, sz);

改为

void* ptr = _aligned_malloc(sz,align_frame_buffer);

int ret = *(static_cast(ptr));

上述方法不正确,由于posix_memalign是Linux下的函数,windows下用_aligned_malloc分配内存,但它们还是有区别,用_aligned_malloc,在运行 test_stream.py时会报:bad dst image pointers 错误。

先改为如下:

在class Picture里增加如下代码,代替Linux下的posix_memalign方法

	#ifdef _WIN32
	static int check_align(size_t align)
	{
		for (size_t i = sizeof(void *); i != 0; i *= 2)
		if (align == i)
			return 0;
		return EINVAL;
	}
	 
	int posix_memalign(void **ptr, size_t align, size_t size)
	{
		if (check_align(align))
			return EINVAL;
	 
		int saved_errno = errno;
		void *p = _aligned_malloc(size, align);
		if (p == NULL)
		{
			errno = saved_errno;
			return ENOMEM;
		}
	 
		*ptr = p;
		return 0;
	}
	#endif

六、项目编译

pip install .

按上述步骤编译就能成功

七、测试

1、修改test_stream.py,如下两个地方,注意视频文件需要复制到测试目录

2、启动 SRS

在windows桌面中点击如下图标即可


3、运行 test_stream.py

python test_stream.py


大功告成!!!

原文链接:,转发请注明来源!