本地文件URI中的GStreamer #符号

1 投票
1 回答
1890 浏览
提问于 2025-04-18 16:40

当我在我的Linux电脑上尝试播放文件名中带有"#"的本地文件时,它显示了:

Error: message about my installation of GStreamer missing plugin /var/tmp/portage/media-libs/gst-plugins-base-0.10.36-r1/work/gst-plugins-base-0.10.36/gst/playback/gstplaybasebin.c(1686): gen_source_element (): /GstPlayBin:player:
No URI handler for file

我用这个来设置URI:

self.player.set_property('uri', 'file://' + filepath)

这里的filepath是指MP3文件的绝对路径,比如说"/home/me/untitled #1.mp3"

有没有什么方法可以解决这个问题或者绕过它呢?

1 个回答

0

filepath 应该进行 URL 编码。

可以使用 urllib.parse.quote()(适用于 Python 3)或者 urllib.quote()(适用于 Python 2)来实现。

这是我使用的代码(Python 3),在 Windows 系统上也应该能正常工作:

def file_to_uri(filename):
    filepath = os.path.abspath(filename) 
    drive, filepath = os.path.splitdrive(filepath)
    filepath = urllib.parse.quote(filepath.replace(os.sep, '/'))
    return 'file://%s%s' % (drive, filepath)

撰写回答