首页 > 解决方案 > GStreamer 应用程序接收器比文件接收器慢得多

问题描述

我正在尝试使用 golang 中的 GStreamer 将 MP3 文件转换为 wave。这是createPipeline()功能:

func createPipeline() (*gst.Pipeline, error) {
    gst.Init(nil)

    // Create a pipeline
    pipeline, err := gst.NewPipelineFromString(
        "appsrc name=src ! decodebin ! audioresample ! audioconvert ! audio/x-raw,format=S16LE,rate=16000 ! wavenc ! appsink name=sink")

    if err != nil {
        return nil, err
    }

    srcElem, err := pipeline.GetElementByName("src")

    if err != nil {
        return nil, err
    }
    src := app.SrcFromElement(srcElem)
    src.SetCallbacks(&app.SourceCallbacks{
        NeedDataFunc: func(self *app.Source, _ uint) {

            // If we've reached the end of the palette, end the stream.
            bytes, _ := os.ReadFile("/tmp/a.mp3")
            buffer := gst.NewBufferFromBytes(bytes)
            self.PushBuffer(buffer)
            src.EndStream()
        },
    })

    sinkElem, err := pipeline.GetElementByName("sink")

    if err != nil {
        return nil, err
    }
    sink := app.SinkFromElement(sinkElem)
    sink.SetCallbacks(&app.SinkCallbacks{
        // Add a "new-sample" callback
        NewSampleFunc: func(sink *app.Sink) gst.FlowReturn {

            // Pull the sample that triggered this callback
            sample := sink.PullSample()
            if sample == nil {
                return gst.FlowEOS
            }

            os.WriteFile("a.wav", sample.GetBuffer().Bytes(), 0644)

            return gst.FlowOK
        },
    })

    return pipeline, nil
}

我有两个选择。第一个是使用filesink元素将结果保存到文件,第二个是使用appsink元素并获取样本并写入文件。我认为这两种方法的性能应该几乎相同。但是当我使用appsink它时,它比filesink. 我正在使用GStreamer 的这个绑定

标签: gogstreamer

解决方案


您可以尝试延迟跟踪器来检查管道元素之间的延迟。如果两条管道都一样,可能是磁盘io的原因,可以通过在写入磁盘前缓冲来解决。


推荐阅读