GStreamer的appsink比filesink慢得多。

huangapple go评论91阅读模式
英文:

GStreamer appsink is much more slower than filesink

问题

我正在尝试使用Golang中的GStreamer将MP3文件转换为Wave格式。以下是createPipeline()函数的代码:

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

    // 创建一个管道
    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) {

            // 如果已经到达调色板的末尾,结束流。
            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{
        // 添加一个“new-sample”回调函数
        NewSampleFunc: func(sink *app.Sink) gst.FlowReturn {

            // 获取触发此回调的样本
            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的这个Go绑定库

英文:

I'm trying to convert an MP3 file to wave using GStreamer in golang. Here is the createPipeline() function:

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
}

I had two choices. The first one was using a filesink element to save the result to a file and the second one was to use a appsink element and to get the samples and write to file. I thought the performance should be almost the same for these two approaches. But when I used appsink it took much more langer than filesink. I'm using this binding of GStreamer for go.

答案1

得分: 2

请注意,appsink 默认会与时钟同步,而 filesink 不会。因此,当你使用 appsink 时,缓冲区将与管道时钟同步 - 这将使得你的管道以实时方式运行(而不是更快)。将你的 appsinksync 属性设置为 false.. ! appsink name=sink sync=false

英文:

Note that appsink syncs to the clock by default while filesink does not. So when you use appsink the buffers will be synchronized to the pipeline clock - this will make your pipeline run in real-time (and not faster). Set the sync property of your appsink to false: .. ! appsink name=sink sync=false.

答案2

得分: 1

你可以尝试使用延迟追踪器来检查管道中各个元素之间的延迟。如果两个管道的延迟相同,那可能是由于磁盘IO引起的,可以通过在写入磁盘之前进行缓冲来解决。

英文:

You can try latency tracer for checking the latency between elements of the pipeline. If it is the same for both pipelines, it is probably due to disk io and can be solved by buffering before writing to disk.

huangapple
  • 本文由 发表于 2021年9月11日 04:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/69137752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定