1

我想下载一个二进制文件(缩略图)并将其存储为临时文件。只要我的班级存在,该文件就应该可以通过其文件名被其他应用程序使用。同时,我的班级应该能够覆盖这个临时文件。

我想在 OBS 中使用它。

这是我的代码的一部分:

#!/usr/bin/env python3
from typing import List, Callable
import obspython as obs
import youtube_dl as ydl
from urllib import request
from pathlib import Path
from sys import stderr
import os
import tempfile

...
class PlayerController:
    '''Simple player controller'''

    def __init__(self, playbackMediaSrc: str, thumbnailImgSrc: str, defaultThumbnailFilePath: str, titleTxtSrc: str, defaultTitleTxt: str, playlist: List[str]) -> None:
        """Constructor

        :param playbackMediaSrc: Playback media source name in OBS
        :param thumbnailImgSrc: Thumbnail image source name in OBS
        :param defaultThumbnailFilePath: Filepath of the default thumbnail image
        :param titleTxtSrc: Title text source name in OBS
        :param defaultTitleTxt: Default title text
        :param playlist: List of music URL's
        """
        self.playbackMediaSrc = playbackMediaSrc
        self.thumbnailImgSrc = thumbnailImgSrc
        self.defaultThumbnailFilePath = defaultThumbnailFilePath
        self.titleTxtSrc = titleTxtSrc
        self.defaultTitleTxt = defaultTitleTxt
        self.playlistModel = PlaylistModel(playlist)
        self.registerMediaSourceSignals()
        self.thumbnailFilePath = None

    ...

    def updateThumbnailSource(self, artURL: str = None) -> None:
        '''Updates the thumbnail image source'''
        if self.thumbnailImgSrc is not None:
            imgSource = obs.obs_get_source_by_name(self.thumbnailImgSrc)
            if imgSource is not None:
                if artURL is not None:
                    print("ART_DOWNLOAD")
                    with request.urlopen(artURL) as response:
                        if self.thumbnailFilePath is None:
                            artFile = tempfile.NamedTemporaryFile(delete=False)
                        else:
                            artFile = open(self.thumbnailFilePath, "wb")
                        artFile.write(response.read())
                        self.thumbnailFilePath = artFile.name
                        artFile.close
                        artFilePath = self.thumbnailFilePath
                    #artFilePath, fileHeader = request.urlretrieve(artURL)
                    #artFilePath = self.defaultThumbnailFilePath
                else:
                    if self.thumbnailFilePath is not None:
                        if os.path.isfile(self.thumbnailFilePath):
                            os.remove(self.thumbnailFilePath)
                    artFilePath = self.defaultThumbnailFilePath
                artFileData = obs.obs_data_create()
                obs.obs_data_set_string(artFileData, "file", artFilePath)
                obs.obs_source_update(imgSource, artFileData)
                obs.obs_data_release(artFileData)
                obs.obs_source_release(imgSource)

    ...

    def stop(self) -> None:
        '''Stops the playback'''
        self.update(None, None, None, None)

    ...

    def update(self, url: str = None, thumbnail: str = None, title: str = None, mrl: str = None) -> None:
        '''Updates all player components'''
        ...
        self.updateThumbnailSource(thumbnail)
        ...

    ...

    def __del__(self):
        '''Destructor'''
        self.stop()

...

我的问题是,第一次下载、存储和访问效果很好。但是第二次 OBS 停止工作并崩溃而没有错误消息。

4

0 回答 0