DEV Community

HarmonyOS
HarmonyOS

Posted on

Audio transcoding and format conversion

Read the original article:Audio transcoding and format conversion

Context

How to convert an audio file from one encoding format/container format to another encoding format/container format.

Description

PCM (Pulse Code Modulation) is the basic uncompressed format of audio data and is the digital audio form that audio codecs can directly process.
Encoding refers to compressing the raw PCM audio data into a corresponding encoding format using a specific algorithm.
Packaging involves placing the already encoded and compressed audio and video data into a file according to a certain format.
The audio transcoding process involves steps such as audio encoding and decoding, and media file repackaging. In HarmonyOS, AVCodec is provided for audio encoding and decoding, AVMuxer for packaging, and AVDemuxer for unpackaging.

Module Supported Formats
Audio decoding formats supported by AVCodec AAC、MPEG(MP3)、FLAC、Vorbis、AMR(amrnb、amrwb)、G711MU、APE、AudioViVid11+、OPUS
Audio encoding formats supported by AVCodec AAC、FLAC、MP3、G711mu、AMR、OPUS
Audio media demultiplexing formats supported by AVDemuxer m4a、aac、mp3、ogg、flac、wav、amr、ape
Audio media encapsulation formats supported by AVMuxer mp4、m4a、mp3、amr、wav、aac

Media files can only be encapsulated into a specified media format if they correspond to the required encoding format. For details, refer to the formats supported by media data encapsulation and parsing.

Solution

Depending on the target audio format to which the conversion is needed, transcoding requirements can be divided into two scenarios: the first scenario is when the target audio format is an encapsulation format; the second scenario is when the target audio format is an encoding format.

Scenario 1: Converting to a Specific Container Format
The audio format is a container format, which needs to be converted to another container format. For example, when using AVPlayer to record audio, an audio file in.m4a format is obtained. Now, this file needs to be converted to.amr format. According to the flowchart, the corresponding encoding format of the.m4a file after demultiplexing is AAC (see case 1). First, the container format is demultiplexed (Demuxer) to obtain AAC-encoded data, which is then decoded (Decode) to obtain PCM audio data. The audio encoding format of the.amr file is AMR. Therefore, the PCM data is encoded (Encode) into AMR format and then multiplexed (Muxer) into an.amr file.
If the encoding format after decoding the original container format is the same as the encoding format supported by the target container format, then only container conversion is required, and no transcoding is needed.
The audio format is PCM raw data, which is encoded and encapsulated into an.mp3 file.
To convert PCM audio data into an.mp3 file, see case 2 in the flowchart. The PCM audio data is encoded (Encode) and compressed into MP3 format, and then multiplexed (Muxer) into an.mp3 file.

Scenario 2: Converting to a Specific Encoding Format
In a real-time voice transmission system, the AMR-NB encoding format collected by the device needs to be converted to the OPUS encoding format. See case 3 in the flowchart, where the AMR-NB encoding format is converted to the OPUS encoding format. This requires decoding (Decode) the data into PCM format and then encoding (Encode) it into the OPUS encoding format.
In case 4 of the flowchart, when the playback software plays an MP3 audio file, the audio needs to be demultiplexed (Demuxer) into the MPEG (MP3) encoding format and then decoded (Decode) to obtain PCM audio data, which is then sent to the hardware speaker for playback.
The above cases only list a few typical scenarios. For specific conversions, please choose container conversion or transcoding based on actual needs.
For detailed information on audio and video encoding and decoding, refer to the sections on audio encoding/decoding. For details on container parsing, refer to the sections on media file encapsulation and parsing.

Key Takeaways

The general steps for audio format conversion are demuxing -> decoding -> PCM -> encoding -> muxing. The specific steps involved depend on whether the source and target formats are encoding formats or container formats. If both the input and output are encoding formats, then the demuxing and muxing steps are not required.

Written by Hatice Akyel

Top comments (0)