URLhttps://learnscript.net/zh/obs-python-scripting/sources/filters-and-transitions/
    复制链接转到说明  示例

    如何操作 OBS 滤镜,转场特效

    我被代码海扁署名-非商业-禁演绎
    阅读 9:43·字数 2915·更新 

    前提

    阅读本节的前提是对 OBS 来源有所掌握,你可以查看如何操作 OBS 来源?来源对象介绍一节来了解相关信息。

    OBS 滤镜和转场特效

    OBS 的滤镜和转场特效是特殊的来源,OBS 的输入来源(OBS_SOURCE_TYPE_INPUT)可以拥有滤镜,以针对输入来源添加更多的效果,比如,改变颜色,调整声音等。场景中的场景项可以拥有显示和隐藏两种转场特效,比如,淡入淡出,滑出等。

    如何获取和设置滤镜和转场特效的相关属性?

    obspython模块没有为每一种滤镜和转场特效,提供获取或设置其相关属性的函数,比如色彩校正中的颜色叠加,你需要修改滤镜和转场特效的来源设置对象,并使用obs_source_update函数进行更新,以完成此目标。

    如何创建滤镜和转场特效对象?

    滤镜和转场特效作为一种来源,可以通过函数obs_source_createobs_source_create_private创建,这需要给出滤镜或转场特效对应的来源类型标识符,比如色彩校正的来源类型标识符为color_filter,滑出的来源类型标识符为swipe_transition

    来源

    关于设置 OBS 来源的设置,你可以查看获取和设置 OBS 来源的设置一段。

    添加和移除输入来源的 OBS 滤镜

    当你创建了新的滤镜后,可通过obspython模块的obs_source_filter_add函数,将其添加至某个输入来源。对于已经添加的来源,可通过obspython模块的obs_source_filter_remove函数将其移除。

    obs_source_filter_add(source, filter)
    obs_source_filter_remove(source, filter)

    source 参数

    source参数为需要添加或移除滤镜的输入来源对象。

    filter 参数

    filter参数为需要添加或移除的滤镜对象。

    获取输入来源中的 OBS 滤镜

    输入来源可拥有多个滤镜,obspython模块的obs_source_get_filter_by_name函数,可根据滤镜名称获取指定的滤镜对象,如果滤镜不存在,则会返回空值None

    obs_source_get_filter_by_name(source, name)

    source 参数

    source参数为包含滤镜的输入来源对象。

    name 参数

    name参数为需要获取的滤镜的名称。

    使用 obs_source_release 函数释放获取的滤镜对象

    对于使用obs_source_get_filter_by_name函数获取的来源对象,需要通过obs_source_release释放引用,否则 OBS 可能会出现错误。

    在下面的代码中,我们通过obs_source_get_filter_by_name函数判断文字(GDI+)中是否存在名称为blue的滤镜,如果不存在,则创建并通过obs_source_filter_add函数添加。

    filters_and_transitions.py
    # 导入模块 obspython
    import obspython as obs
    
    def test(props, prop): # 获取文字(GDI+)来源中名称为 blue 滤镜 welcome = obs.obs_get_source_by_name('Welcome') blue = obs.obs_source_get_filter_by_name(welcome, 'blue')
    if not blue: # 如果 blue 滤镜不存在,则创建添加该滤镜 settings = obs.obs_data_create_from_json('{"color_multiply":' + str(0xFF0000) + '}') blue = obs.obs_source_create('color_filter_v2', 'blue', settings, None) obs.obs_source_filter_add(welcome, blue)
    # 释放 OBS 数据设置对象 obs.obs_data_release(settings)
    # 释放 OBS 来源对象 obs.obs_source_release(blue) obs.obs_source_release(welcome)
    # 为脚本添加一个用于测试的按钮,回调函数为 test def script_properties(): props = obs.obs_properties_create() obs.obs_properties_add_button(props, 'test', '测试', test) return props

    复制输入来源中的 OBS 滤镜至另一个输入来源

    obspython模块的obs_source_copy_filters函数,可用于将一个输入来源中的滤镜,复制到另一个输入来源,复制产生的新滤镜的名称将被修改,如果他们与目标来源中的滤镜名称相同的话。

    obspython模块的obs_source_copy_single_filter函数,可用于将一个滤镜复制到某个输入来源,复制产生的新滤镜的名称将被修改,如果他们与目标来源中的滤镜名称相同的话。

    obs_source_copy_filters(dst, src)
    obs_source_copy_single_filter(dst, filter)

    dst 参数

    dst参数为接受新滤镜的输入来源对象。

    src 参数

    src参数为被复制滤镜的输入来源对象。

    filter 参数

    filter参数为被复制滤镜对象。

    下面的代码,将复制来源Welcome中的滤镜至来源Bye

    filters_and_transitions.py
    def test(props, prop):
    	# …
    	# 复制 Welcome 中的滤镜到 Bye
    	bye = obs.obs_get_source_by_name('Bye')
    	obs.obs_source_copy_filters(bye, welcome)
    	# 释放 OBS 来源对象
    	obs.obs_source_release(bye)

    获取输入来源中 OBS 滤镜的个数

    obspython模块的obs_source_filter_count函数,可用于获取输入来源中的滤镜的个数。

    obs_source_filter_count(source)

    source 参数

    source参数为需要计算滤镜个数的输入来源对象。

    备份和还原输入来源中的 OBS 滤镜

    obspython模块的obs_source_backup_filters函数,可用于将输入来源中的滤镜备份至一个数据数组对象并返回。

    obspython模块的obs_source_restore_filters函数,可用于将数据数组对象中的滤镜还原至输入来源。

    obs_source_backup_filters(source)
    obs_source_restore_filters(source, array)

    source 参数

    source参数为需要备份或还原滤镜的输入来源对象。

    array 参数

    array参数为备份了滤镜的数据数组对象。

    使用 obs_data_array_release 函数释放作为滤镜备份的数据数组对象

    对于使用obs_source_backup_filters函数获取的数据数组对象,需要通过obs_data_array_release释放引用,否则 OBS 可能会出现错误。

    排序输入来源中的 OBS 滤镜

    obspython模块的obs_source_filter_set_order函数,可将滤镜的位置向上,向下移动,以及将滤镜移动至开头或末尾。

    obspython模块的obs_source_filter_get_indexobs_source_filter_set_index函数(需要 OBS 版本为 30.0 或更高),可将滤镜移动到指定索引位置。

    obs_source_filter_set_order(source, filter, movement)
    obs_source_filter_get_index(source, filter)
    obs_source_filter_set_index(source, filter, index)

    source 参数

    source参数为需要移动滤镜位置的输入来源对象。

    filter 参数

    filter参数为需要移动的滤镜对象。

    movement 参数

    movement参数为移动方式,可使用如下obspython模块变量进行设置,OBS_ORDER_MOVE_UP表示向上移动,OBS_ORDER_MOVE_DOWN表示向下移动,OBS_ORDER_MOVE_TOP表示移动至开头,OBS_ORDER_MOVE_BOTTOM表示移动至末尾。

    index 参数

    index参数为目标位置的索引,0表示末尾(倒数第一),倒数第二为1

    在下面的代码中,我们使用obs_source_filter_count函数获取来源Welcome的滤镜个数,并根据个数将blue滤镜移动至第二的位置。

    filters_and_transitions.py
    def test(props, prop):
    	# …
    	if not blue:
    		# …
    		# 获取来源的滤镜个数,并将 blue 滤镜移动至第二的位置
    		count = obs.obs_source_filter_count(welcome)
    		if count > 1:
    			obs.obs_source_filter_set_index(welcome, blue, count - 2)
    		# …

    获取和设置场景项的 OBS 转场特效

    如果希望获取或设置场景项的转场特效,需要通过obspython模块提供的与场景项有关的函数进行,不过,这里将介绍另一个函数obs_get_transition_by_name,他可以获取指定名称的转场特效。对于由用户设置的转场特效,其名称格式通常为<item> 显示转场特效<item> 隐藏转场特效,其中item为场景项的名称。

    obs_get_transition_by_name(name)

    name 参数

    name参数为需要获取的 OBS 转场特效对象的名称。

    使用 obs_source_release 函数释放获取的转场特效对象

    对于使用obs_get_transition_by_name函数获取的转场特效对象,需要通过obs_source_release释放引用,否则 OBS 可能会出现错误。

    场景项,分组

    想要了解其他获取或设置场景项转场特效的函数,你可以查看获取和设置 OBS 场景项的转场特效一段。

    filters_and_transitions.py
    def test(props, prop):
    	# …
    	# 获取来源 Welcome 的隐藏转场特效
    	t = obs.obs_get_transition_by_name('Welcome 隐藏转场特效')
    	obs.obs_source_release(t)

    源码

    filters_and_transitions.py·codebeatme/obs-python-scripting·GitHub