使用 GStreamer 将 FLAC 转换为 MP3 Python?

发布于 2024-12-05 01:56:30 字数 1062 浏览 1 评论 0原文

这是我试图复制的命令:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2024-12-12 01:56:30

Gstreamer 使用 GObject 作为框架,因此您需要运行 gobject.MainLoop() 来启动管道中的消息流:

import gobject
import gst
pipeline = gst.parse_launch('filesrc location="test.flac" ! flacdec ! lame ! filesink location="test.mp3"')
pipeline.set_state(gst.STATE_PLAYING)

gobject.threads_init()
gobject.MainLoop().run()

在第二个示例中,您还需要运行 MainLoop 并链接所有管道元素(例如element_link_many)。
您仅将源连接到接收器,因此您的实际管道只是 filesrc !文件接收器。

这是更正后的代码:

import gobject
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)
gst.element_link_many(source, decoder, encoder, sink)

converter.set_state(gst.STATE_PLAYING)

gobject.threads_init()
gobject.MainLoop().run()

Gstreamer uses GObject as a framework, so you need to run gobject.MainLoop() to start message flow in pipeline:

import gobject
import gst
pipeline = gst.parse_launch('filesrc location="test.flac" ! flacdec ! lame ! filesink location="test.mp3"')
pipeline.set_state(gst.STATE_PLAYING)

gobject.threads_init()
gobject.MainLoop().run()

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:

import gobject
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)
gst.element_link_many(source, decoder, encoder, sink)

converter.set_state(gst.STATE_PLAYING)

gobject.threads_init()
gobject.MainLoop().run()
是你 2024-12-12 01:56:30

有些人最终在这个答案中寻找命令行/bash 解决方案。这是一个很好的转换脚本。

#!/bin/bash

#Take a lossless flac audio track from and transcode it as a constant rate mp3 playable on some older audio equipment that can't play mp4s or variable bit rate mp3's.

if [ -z "$1" ];then
   echo usage: $0 [infile.flac]
   exit
fi

INFILE=$(basename $1)
WKDIR=$(dirname $1)
OUTFILE=$WKDIR/$( echo $INFILE | sed 's/\.flac//').mp3

#play ===================
#gst- launch filesrc location=1.flac \
#! flacdec \
#! autoaudiosink

#transcode ==============
gst-launch filesrc location=$INFILE \
! queue \
! flacdec \
! audioconvert \
! audioresample \
! lamemp3enc quality=2 target=bitrate bitrate=192 cbr=true \
! id3v2mux \
! filesink location=$OUTFILE

Some people end up at this answer looking for the command line / bash solution. Here is a good conversion script.

#!/bin/bash

#Take a lossless flac audio track from and transcode it as a constant rate mp3 playable on some older audio equipment that can't play mp4s or variable bit rate mp3's.

if [ -z "$1" ];then
   echo usage: $0 [infile.flac]
   exit
fi

INFILE=$(basename $1)
WKDIR=$(dirname $1)
OUTFILE=$WKDIR/$( echo $INFILE | sed 's/\.flac//').mp3

#play ===================
#gst- launch filesrc location=1.flac \
#! flacdec \
#! autoaudiosink

#transcode ==============
gst-launch filesrc location=$INFILE \
! queue \
! flacdec \
! audioconvert \
! audioresample \
! lamemp3enc quality=2 target=bitrate bitrate=192 cbr=true \
! id3v2mux \
! filesink location=$OUTFILE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文