Home > Backend Development > Golang > Golang and FFmpeg: How to achieve audio noise reduction and distortion repair

Golang and FFmpeg: How to achieve audio noise reduction and distortion repair

PHPz
Release: 2023-09-27 09:37:02
Original
1125 people have browsed it

Golang与FFmpeg: 如何实现音频降噪和失真修复

Golang and FFmpeg: How to achieve audio noise reduction and distortion repair

Introduction:
In the field of audio processing, noise reduction and distortion repair are two very important task. Noise reduction can remove noise in the audio, improve the sound quality, and make the audio clearer; while distortion repair can repair distortion introduced due to transmission or recording, so that the audio can restore its original sound quality. This article will introduce how to use Golang and FFmpeg libraries to implement audio noise reduction and distortion repair, with specific code examples.

1. Install and configure FFmpeg

First, we need to install the FFmpeg library and configure the environment. On Linux systems, you can use package management tools for installation, such as:

$ sudo apt-get install ffmpeg
Copy after login

On Windows systems, you can download the installation package from the official website of FFmpeg for installation.

After the installation is complete, we need to introduce the FFmpeg library into Golang. You can use Go’s package management tool for installation, such as:

$ go get github.com/giorgisio/goav/avformat
$ go get github.com/giorgisio/goav/avutil
Copy after login

Then, introduce the FFmpeg library into the code:

import (
    "github.com/giorgisio/goav/avformat"
    "github.com/giorgisio/goav/avutil"
)
Copy after login

2. Implementation of audio noise reduction

Audio reduction Noise can be achieved by removing the noise component in the spectrum. In FFmpeg, we can use the Denoise audio filter for noise reduction.

The specific code is as follows:

func denoise(inputFile string, outputFile string) error {
    inputFormat := avformat.AvFindInputFormat("wav")
    avformat.AvRegisterAll()

    // 打开输入文件
    inputContext := avformat.AvformatAllocContext()
    if avformat.AvformatOpenInput(&inputContext, inputFile, inputFormat, nil) != 0 {
        return fmt.Errorf("failed to open input file")
    }
    defer avformat.AvformatCloseInput(&inputContext)

    // 打开输出文件
    outputContext := avformat.AvformatAllocContext()
    if avformat.AvformatAllocOutputContext2(&outputContext, nil, "wav", outputFile) != 0 {
        return fmt.Errorf("failed to create output context")
    }
    defer avformat.AvformatFreeContext(outputContext)
    
    // 寻找音频流
    if avformat.AvformatFindStreamInfo(inputContext, nil) < 0 {
        return fmt.Errorf("failed to find stream info")
    }

    audioStreamIndex := -1
    for i := 0; i < len(inputContext.Streams); i++ {
        if inputContext.Streams[i].CodecParameters.GetCodecType() == avformat.AVMEDIA_TYPE_AUDIO {
            audioStreamIndex = i
            break
        }
    }

    if audioStreamIndex == -1 {
        return fmt.Errorf("failed to find audio stream")
    }

    audioStream := inputContext.Streams[audioStreamIndex]
    codecParameters := audioStream.CodecParameters

    // 初始化解码器
    decoder := avformat.AvcodecFindDecoder(codecParameters.GetCodecId())
    if decoder == nil {
        return fmt.Errorf("failed to find decoder")
    }
    
    codecContext := avformat.AvcodecAllocContext3(decoder)
    if codecContext == nil {
        return fmt.Errorf("failed to allocate codec context")
    }

    if avformat.AvcodecParametersToContext(codecContext, codecParameters) < 0 {
        return fmt.Errorf("failed to copy codec parameters")
    }

    if avformat.AvcodecOpen2(codecContext, decoder, nil) < 0 {
        return fmt.Errorf("failed to open decoder")
    }

    // 初始化音频处理滤镜
    filters := fmt.Sprintf("anullsrc=cl=stereo|cr=44100,ade noise" +
        "=all_mode=0:amount=0.8,f=format=s16p:samplerate=44100" +
        ":sample_fmts=s16", codecParameters.SampleRate)

    audioFilterGraph := avutil.AvfilterGraphAlloc()

    if avutil.AvfilterGraphParse2(audioFilterGraph, filters, nil) < 0 {
        return fmt.Errorf("failed to parse filter graph")
    }

    // 初始化音频转换器
    audioConvertContext := avutil.AvAudioResampleInit(codecContext.Channels,
        codecContext.SampleRate, codecParameters.SampleRate,
        codecParameters.Channels, avutil.SampleFormat(codecParameters.Format),  
        avutil.SampleFormat(avutil.AV_SAMPLE_FMT_S16), 0, 0, nil)

    if audioConvertContext == nil {
        return fmt.Errorf("failed to init audio resampler")
    }

    // 初始化输出编码器
    outputCodec := avformat.AvcodecFindEncoder(avformat.CodecId(codecParameters.GetCodecId()))
    if outputCodec == nil {
        return fmt.Errorf("failed to find output encoder")
    }

    outputCodecContext := avformat.AvcodecAllocContext3(outputCodec)
    if outputCodecContext == nil {
        return fmt.Errorf("failed to allocate output codec context")
    }

    outputCodecContext.SampleRate = codecParameters.SampleRate
    outputCodecContext.Channels = codecParameters.Channels
    outputCodecContext.SampleFmt = avutil.AV_SAMPLE_FMT_S16
    outputCodecContext.BitRate = codecParameters.BitRate

    if avformat.AvcodecOpen2(outputCodecContext, outputCodec, nil) < 0 {
        return fmt.Errorf("failed to open output encoder")
    }

    // 初始化输出流
    outputStream := outputContext.AvformatNewStream(outputCodec)
    if outputStream == nil {
        return fmt.Errorf("failed to create output stream")
    }
    outputStream.CodecParameters = codecParameters

    // 写入输出文件头
    if avformat.AvformatWriteHeader(outputContext, nil) < 0 {
        return fmt.Errorf("failed to write output header")
    }

    // 音频流降噪并写入输出文件
    packet := avformat.AvPacketAlloc()
    for avformat.AvReadFrame(inputContext, packet) >= 0 {
        if packet.StreamIndex == audioStreamIndex {
            // 解码音频帧
            frame := avutil.AvFrameAlloc()
            if avformat.AvcodecSendPacket(codecContext, packet) == 0 {
                for avformat.AvcodecReceiveFrame(codecContext, frame) >= 0 {
                    // 音频降噪
                    avutil.AvBuffersrcAddFrameFlags(audioFilterGraph.GetInputs()[0], frame, 0)
                    for avutil.AvBuffersinkGetFrame(audioFilterGraph.GetOutputs()[0].GetFilterContext(), frame) >= 0 {
                        // 音频转换
                        avutil.AvAudioResampleConvert(audioConvertContext, &frame.Data, frame.GetExtendedData(), 
                            frame.GetNbSamples(), frame.Channels, frame.Format, frame.SampleRate, 0)

                        // 编码音频
                        if avformat.AvcodecSendFrame(outputCodecContext, frame) == 0 {
                            for avformat.AvcodecReceivePacket(outputCodecContext, packet) >= 0 {
                                packet.StreamIndex = outputStream.Index
                                avformat.AvpacketRescaleTs(packet, codecContext.TimeBase, outputStream.TimeBase)
                                avformat.AvInterleavedWriteFrame(outputContext, packet)
                                avformat.AvPacketUnref(packet)
                            }
                        }
                    }
                }
            }
            avutil.AvFrameFree(&frame)
        }
        avformat.AvPacketUnref(packet)
    }

    // 写入输出文件尾
    avformat.AvWriteTrailer(outputContext)

    return nil
}
Copy after login

3. Implementation of audio distortion repair

Audio distortion repair can restore the original sound quality through some algorithms. In FFmpeg, we can use audio filters that repair pitch to achieve distortion repair.

The specific code is as follows:

func distort(inputFile string, outputFile string) error {
    inputFormat := avformat.AvFindInputFormat("wav")
    avformat.AvRegisterAll()

    // 打开输入文件
    inputContext := avformat.AvformatAllocContext()
    if avformat.AvformatOpenInput(&inputContext, inputFile, inputFormat, nil) != 0 {
        return fmt.Errorf("failed to open input file")
    }
    defer avformat.AvformatCloseInput(&inputContext)

    // 打开输出文件
    outputContext := avformat.AvformatAllocContext()
    if avformat.AvformatAllocOutputContext2(&outputContext, nil, "wav", outputFile) != 0 {
        return fmt.Errorf("failed to create output context")
    }
    defer avformat.AvformatFreeContext(outputContext)
    
    // 寻找音频流
    if avformat.AvformatFindStreamInfo(inputContext, nil) < 0 {
        return fmt.Errorf("failed to find stream info")
    }

    audioStreamIndex := -1
    for i := 0; i < len(inputContext.Streams); i++ {
        if inputContext.Streams[i].CodecParameters.GetCodecType() == avformat.AVMEDIA_TYPE_AUDIO {
            audioStreamIndex = i
            break
        }
    }

    if audioStreamIndex == -1 {
        return fmt.Errorf("failed to find audio stream")
    }

    audioStream := inputContext.Streams[audioStreamIndex]
    codecParameters := audioStream.CodecParameters

    // 初始化解码器
    decoder := avformat.AvcodecFindDecoder(codecParameters.GetCodecId())
    if decoder == nil {
        return fmt.Errorf("failed to find decoder")
    }
    
    codecContext := avformat.AvcodecAllocContext3(decoder)
    if codecContext == nil {
        return fmt.Errorf("failed to allocate codec context")
    }

    if avformat.AvcodecParametersToContext(codecContext, codecParameters) < 0 {
        return fmt.Errorf("failed to copy codec parameters")
    }

    if avformat.AvcodecOpen2(codecContext, decoder, nil) < 0 {
        return fmt.Errorf("failed to open decoder")
    }

    // 初始化音频处理滤镜
    filters := fmt.Sprintf("asetrate=44100,aresample=44100,atempo=1")

    audioFilterGraph := avutil.AvfilterGraphAlloc()

    if avutil.AvfilterGraphParse2(audioFilterGraph, filters, nil) < 0 {
        return fmt.Errorf("failed to parse filter graph")
    }

    // 初始化输出编码器
    outputCodec := avformat.AvcodecFindEncoder(avformat.CodecId(codecParameters.GetCodecId()))
    if outputCodec == nil {
        return fmt.Errorf("failed to find output encoder")
    }

    outputCodecContext := avformat.AvcodecAllocContext3(outputCodec)
    if outputCodecContext == nil {
        return fmt.Errorf("failed to allocate output codec context")
    }

    outputCodecContext.SampleRate = codecParameters.SampleRate
    outputCodecContext.Channels = codecParameters.Channels
    outputCodecContext.SampleFmt = avutil.AV_SAMPLE_FMT_S16
    outputCodecContext.BitRate = codecParameters.BitRate

    if avformat.AvcodecOpen2(outputCodecContext, outputCodec, nil) < 0 {
        return fmt.Errorf("failed to open output encoder")
    }

    // 初始化输出流
    outputStream := outputContext.AvformatNewStream(outputCodec)
    if outputStream == nil {
        return fmt.Errorf("failed to create output stream")
    }
    outputStream.CodecParameters = codecParameters

    // 写入输出文件头
    if avformat.AvformatWriteHeader(outputContext, nil) < 0 {
        return fmt.Errorf("failed to write output header")
    }

    // 音频流失真修复并写入输出文件
    packet := avformat.AvPacketAlloc()
    for avformat.AvReadFrame(inputContext, packet) >= 0 {
        if packet.StreamIndex == audioStreamIndex {
            // 解码音频帧
            frame := avutil.AvFrameAlloc()
            if avformat.AvcodecSendPacket(codecContext, packet) == 0 {
                for avformat.AvcodecReceiveFrame(codecContext, frame) >= 0 {
                    // 音频失真修复
                    avutil.AvBuffersrcAddFrameFlags(audioFilterGraph.GetInputs()[0], frame, 0)
                    for avutil.AvBuffersinkGetFrame(audioFilterGraph.GetOutputs()[0].GetFilterContext(), frame) >= 0 {
                        // 编码音频
                        if avformat.AvcodecSendFrame(outputCodecContext, frame) == 0 {
                            for avformat.AvcodecReceivePacket(outputCodecContext, packet) >= 0 {
                                packet.StreamIndex = outputStream.Index
                                avformat.AvpacketRescaleTs(packet, codecContext.TimeBase, outputStream.TimeBase)
                                avformat.AvInterleavedWriteFrame(outputContext, packet)
                                avformat.AvPacketUnref(packet)
                            }
                        }
                    }
                }
            }
            avutil.AvFrameFree(&frame)
        }
        avformat.AvPacketUnref(packet)
    }

    // 写入输出文件尾
    avformat.AvWriteTrailer(outputContext)

    return nil
}
Copy after login

Summary:

By using Golang language and FFmpeg library, we can easily realize the functions of audio noise reduction and distortion repair. In terms of noise reduction, we use Denoise audio filters to remove noise; in terms of distortion repair, we use audio filters that repair pitch to restore the original sound quality of the audio. The above are specific code examples, I hope they are helpful to you.

The above is the detailed content of Golang and FFmpeg: How to achieve audio noise reduction and distortion repair. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template