使用 GStreamer 将 FLAC 转换为 MP3 Python?
这是我试图复制的命令:
gst-launch filesrc location=test.flac ! flacdec ! lame ! filesink location=test.mp3
当我运行此命令时,它运行良好。我尝试使用 Pythong 绑定来复制此内容,但完全没有运气。我没有收到任何这些脚本的错误,但它们没有按预期工作:
当我运行此脚本时,我只是得到一个空的 MP3 文件:
import gst
pipeline = gst.parse_launch('filesrc location="test.flac" ! flacdec ! lame ! filesink location="test.mp3"')
pipeline.set_state(gst.STATE_PLAYING)
当我运行此脚本时,我得到一个损坏的 MP3 文件:
import gst
converter = gst.Pipeline('converter')
source = gst.element_factory_make('filesrc', 'file-source')
source.set_property('location', 'test.flac')
decoder = gst.element_factory_make('flacdec', 'decoder')
encoder = gst.element_factory_make('lame', 'encoder')
sink = gst.element_factory_make('filesink', 'sink')
sink.set_property('location', 'test.mp3')
converter.add(source, decoder, encoder, sink)
source.link(sink)
converter.set_state(gst.STATE_PLAYING)
任何人都知道什么我做错了吗?
Here's the command I'm trying to replicate:
gst-launch filesrc location=test.flac ! flacdec ! lame ! filesink location=test.mp3
When I run this command it works beautifully. I've tried to replicate this using the Pythong bindings with no luck at all. I don't get any errors with either of these scripts, but they don't work as expected:
When I run this script I just get an empty MP3 file:
import gst
pipeline = gst.parse_launch('filesrc location="test.flac" ! flacdec ! lame ! filesink location="test.mp3"')
pipeline.set_state(gst.STATE_PLAYING)
When I run this script I get a corrupt MP3 file:
import gst
converter = gst.Pipeline('converter')
source = gst.element_factory_make('filesrc', 'file-source')
source.set_property('location', 'test.flac')
decoder = gst.element_factory_make('flacdec', 'decoder')
encoder = gst.element_factory_make('lame', 'encoder')
sink = gst.element_factory_make('filesink', 'sink')
sink.set_property('location', 'test.mp3')
converter.add(source, decoder, encoder, sink)
source.link(sink)
converter.set_state(gst.STATE_PLAYING)
Anyone know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Gstreamer 使用 GObject 作为框架,因此您需要运行 gobject.MainLoop() 来启动管道中的消息流:
在第二个示例中,您还需要运行 MainLoop 并链接所有管道元素(例如
element_link_many
)。您仅将源连接到接收器,因此您的实际管道只是
filesrc !文件接收器。
这是更正后的代码:
Gstreamer uses GObject as a framework, so you need to run
gobject.MainLoop()
to start message flow in pipeline:In the second example you also need to run MainLoop and link all the pipeline elements (e.g. with
element_link_many
).You connected only source to sink, so your actual pipeline is just
filesrc ! filesink
.Here is corrected code:
有些人最终在这个答案中寻找命令行/bash 解决方案。这是一个很好的转换脚本。
Some people end up at this answer looking for the command line / bash solution. Here is a good conversion script.