Python controls obs to achieve seamless scene switching! obs-websocket-py

Preface

Recently I have been studying the twin digital human wav2lip. The current results can directly input high-definition mouth shapes, and the 2070 graphics card can output 1 minute of audio in 2.6 minutes. Logically, we can achieve a ratio of 1 to 1.3 in live streaming, so we will start studying live streaming now. It logically involves seamless switching. I saw an article on csdn and asked for VIP unlocking. . . Then do your own research! Here we use my current logic to achieve seamless switching!

As an example

Let’s sort out my logic, push a video in scene 1 through obs, and loop a video that doesn’t speak and shuts up. After generating the talking video, control obs to modify the source video path of scene 2 and automatically switch to scene 2 for playback. Then, after the video of scene 2 is played, it will automatically switch back to the non-speaking video of scene 1~!

bring it on! Just do it!

obs-api documentation: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#getmediainputstatus

 Don’t ask me why, because there are too few relevant ready-made wheels from Baidu! It’s best to read the documentation to choose and implement your own business, it’s very simple!

1. Create obs-ws server

 2. Create two scenes and add a video source to each scene

 SceneName scene adds a MediaSourceNameA source and adds video A

 SceneName1 scene adds a MediaSourceNameB source and adds video A [when the digital human video is generated, replace this path with py]

Video A of MediaSourceNameA of SceneName scene is set to loop.

 

 The purpose is to play video A of the pushed SceneName scene MediaSourceNameA by default, that is, the video when no talking is done.

3. Python controls obs to seamlessly switch scenes

Install dependencies

pip install obs-websocket-py

 connectobs

# 连接OBS
ws = obsws('192.168.31.73', 4455, 'YqShGFfdYaGxG7DG')
ws.connect()

This address comes from here

Tools-》obs-websocket settings 

 Click Show Connection Information to see the IP and password.

 After the digital human video is generated, execute the following code!

        # 修改MediaSourceNameB来源的视频路径
        ws.call(requests.SetInputSettings(inputName="MediaSourceNameB", inputSettings={
            "local_file": os.path.abspath(video_path)
        }))
        # 激活SceneName1场景播放视频
        ws.call(requests.SetCurrentProgramScene(sceneName='SceneName1'))

 You can monitor whether the video of the MediaSourceNameB scene has finished playing, and when the playback is completed, switch back to the looped video of the SceneName scene.

        while True:
            time.sleep(0.1)
            # 监听MediaSourceNameB视频播放状态
            status = ws.call(requests.GetMediaInputStatus(inputName="MediaSourceNameB")).datain
            print(status)
            # 播放完毕则切换回激活循环播放的SceneName场景视频
            if status['mediaState'] == 'OBS_MEDIA_STATE_ENDED':
                ws.call(requests.SetCurrentProgramScene(sceneName='SceneName'))
                break

 

Close websocket connection

# 断开连接
ws.disconnect()

4. Implement custom logic

What? Confused? ? ? It’s not difficult, I’ll teach you!

Open document

https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#getmediainputstatus

For example, query the video playback status

ws.call(requests.GetMediaInputStatus(inputName="MediaSourceNameB")).datain

 Search for GetMediaInputStatus, you can see the same name in the documentation, and it also has an introduction to its use. If you don’t know English, please translate it!

It will tell you what it does, what parameters you pass, and what response you get!

In other words, which method you want to use in this document is as follows:

requests.XXX

As for passing parameter names, it’s in the documentation! Just write it correspondingly in the method!

requests.XXX(XXX=???,XXX=???)

Must be wrapped by ws.call()

The object is returned by default, .datain returns data!

 

 

 Notice!

For the modified method, the object will be passed. The document does not detail the parameter names and formats in the object. This is a bit confusing! However, it corresponds to a query method. Just write it according to the content format of the query results! 

alright! That’s it for the whole thing, just explore it yourself according to my method! As for how to push streams, this is the basics of OBS, just download it on Baidu yourself! ! !

Guess you like

Origin blog.csdn.net/weixin_47723549/article/details/132211515