0.编码流程图
1前言
本文将会使用上篇文章手动编译的ffmpeg对输入的PCM数据进行AAC编码,但是fdk-aac 对输入的PCM数据是有要求的,如果输入的参数不符合预期,就会报错
2参数要求
参数要求
2.1采样格式
必须是16位整数PCM
2.2 采样率
支持的采样率有(Hz)
8000、11025、12000、16000、22050、24000、32000
44100、48000、64000、88200、96000
3.命令行编码
3.1基本使用
# pcm -> aac
ffmpeg -ar 44100 -ac 2 -f s16le -i in.pcm -c:a libfdk_aac out.aac
# wav -> aac
# 为了简化指令,本文后面会尽量使用in.wav取代in.pcm
ffmpeg -i in.wav -c:a libfdk_aac out.aac
- ar 指定比特率
- ac 指定声道数
- -f 音频样本格式
这三个参数必须要和in.pcm 对应上
- -c:a 设置音频编码(c表示codec编解码器,a表示audio音频),等价写法-codec:a -acodec
songlin@feng-sl ~/audio/aac master ±✚ ffmpeg -ar 44100 -ac 2 -f f32le -i 44100_f32le_2.pcm -c:1 libfdk_aac out.aac
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[f32le @ 0x7fabca010600] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from '44100_f32le_2.pcm':
Duration: 00:00:17.36, bitrate: 2822 kb/s
Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, adts, to 'out.aac':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.91.100 aac
size= 277kB time=00:00:17.36 bitrate= 130.8kbits/s speed=85.7x
video:0kB audio:272kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.880452%
[aac @ 0x7fabcb81cc00] Qavg: 252.198
songlin@feng-sl ~/audio/aac master ±✚ ls
44100_f32le_2.pcm out.aac
PCM:6.1M,AAC:284k
3.2查看ac文件规格
✘ songlin@feng-sl ~/audio/aac master ±✚ ffprobe out.aac
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[aac @ 0x7fbaa5808200] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'out.aac':
Duration: 00:00:16.74, bitrate: 135 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 135 kb/s
3.3 常用参数
从上面可以看到aac文件的比特率是135kb/s,那么这个值我们能修改么?答案是肯定的,可以通过一些输出参数来达到这个目的
- -b:a设置输出比特率,-b:a 48k
✘ songlin@feng-sl ~/audio/aac master ±✚ ffmpeg -ar 44100 -ac 2 -f f32le -i 44100_f32le_2.pcm -c:a libfdk_aac -b:a 48k 48kout.aac
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[f32le @ 0x7fd90480c400] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from '44100_f32le_2.pcm':
Duration: 00:00:17.36, bitrate: 2822 kb/s
Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> aac (libfdk_aac))
Press [q] to stop, [?] for help
Output #0, adts, to '48kout.aac':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Audio: aac (libfdk_aac), 44100 Hz, stereo, s16, 48 kb/s
Metadata:
encoder : Lavc58.91.100 libfdk_aac
size= 103kB time=00:00:17.36 bitrate= 48.4kbits/s speed= 103x
video:0kB audio:103kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
songlin@feng-sl ~/audio/aac master ±✚ o
songlin@feng-sl ~/audio/aac master ±✚ ffprobe 48kout.aac
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[aac @ 0x7fc72b008200] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from '48kout.aac':
Duration: 00:00:17.42, bitrate: 48 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 48 kb/s
- -profile:a 设置输出规格( Value is one of LC, HE-AAC, HE-AACv2, LD, or ELD. Default is LC.)
取值有:
aac_low:Low Complexity AAC (LC),默认值
aac_he:High Efficiency AAC (HE-AAC)
aac_he_v2:High Efficiency AAC version 2 (HE-AACv2)
aac_ld:Low Delay AAC (LD)
aac_eld:Enhanced Low Delay AAC (ELD)
示例
✘ songlin@feng-sl ~/audio/aac master ±✚ ffmpeg -ar 44100 -ac 2 -f f32le -i 44100_f32le_2.pcm -c:a libfdk_aac -profile:a aac_he_v2 -b:a 48k 48k_aac_he_v2_out.aac
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[f32le @ 0x7fd022010a00] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from '44100_f32le_2.pcm':
Duration: 00:00:17.36, bitrate: 2822 kb/s
Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> aac (libfdk_aac))
Press [q] to stop, [?] for help
Output #0, adts, to '48k_aac_he_v2_out.aac':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Audio: aac (libfdk_aac) (HE-AACv2), 44100 Hz, stereo, s16, 48 kb/s
Metadata:
encoder : Lavc58.91.100 libfdk_aac
size= 103kB time=00:00:17.39 bitrate= 48.4kbits/s speed=76.2x
video:0kB audio:103kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
songlin@feng-sl ~/audio/aac master ±✚ ffprobe 48k_aac_he_v2_out.aac
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[aac @ 0x7fcd70810000] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from '48k_aac_he_v2_out.aac':
Duration: 00:00:18.25, bitrate: 46 kb/s
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 46 kb/s
一旦设置了输出规格,会自动设置一个合适的输出比特率(我设置的是48k结果输出了46k)
- -vbr
vbr模式
如果开启了VBR模式,-b:a选项将会被忽略,但-profile:a选项仍然有效
取值范围是0 ~ 5
0:默认值,关闭VBR模式,开启CBR模式(Constant Bit Rate,固定比特率)
1:质量最低(但是音质仍旧很棒)
5:质量最高
VBR | kbps/channel | Audio Object Type |
---|---|---|
1 | 20-32 | LC、HE、HEv2 |
2 | 32-40 | LC、HE、HEv2 |
3 | 48-56 | LC、HE、HEv2 |
4 | 64-72 | LC |
5 | 96-112 | LC |
示例
** -b: a 被忽略例子**
songlin@feng-sl ~/audio/aac master ±✚ ffmpeg -ar 44100 -ac 2 -f f32le -i 44100_f32le_2.pcm -c:a libfdk_aac -vbr 1 -b:a 48k vb1out.aac
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[f32le @ 0x7fa4d0013400] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from '44100_f32le_2.pcm':
Duration: 00:00:17.36, bitrate: 2822 kb/s
Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> aac (libfdk_aac))
Press [q] to stop, [?] for help
[libfdk_aac @ 0x7fa4d1009a00] Note, the VBR setting is unsupported and only works with some parameter combinations
Output #0, adts, to 'vb1out.aac':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Audio: aac (libfdk_aac), 44100 Hz, stereo, s16, 48 kb/s
Metadata:
encoder : Lavc58.91.100 libfdk_aac
size= 149kB time=00:00:17.36 bitrate= 70.4kbits/s speed=89.8x
video:0kB audio:149kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
songlin@feng-sl ~/audio/aac master ±✚ ffprobe vb1out.aac
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[aac @ 0x7fc07100ac00] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'vb1out.aac':
Duration: 00:00:12.49, bitrate: 97 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 97 kb/s
**profile 依然生效**
✘ songlin@feng-sl ~/audio/aac master ±✚ ffmpeg -ar 44100 -ac 2 -f f32le -i 44100_f32le_2.pcm -c:a libfdk_aac -profile:a aac_he_v2 -vbr 1 vb1_profile_out.aac
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[f32le @ 0x7fa64980ca00] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from '44100_f32le_2.pcm':
Duration: 00:00:17.36, bitrate: 2822 kb/s
Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> aac (libfdk_aac))
Press [q] to stop, [?] for help
[libfdk_aac @ 0x7fa64980dc00] Note, the VBR setting is unsupported and only works with some parameter combinations
Output #0, adts, to 'vb1_profile_out.aac':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Audio: aac (libfdk_aac) (HE-AACv2), 44100 Hz, stereo, s16
Metadata:
encoder : Lavc58.91.100 libfdk_aac
size= 70kB time=00:00:17.39 bitrate= 32.8kbits/s speed= 88x
video:0kB audio:70kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
songlin@feng-sl ~/audio/aac master ±✚ ffprobe vb1_profile_out.aac
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
[aac @ 0x7f8d3b80ca00] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'vb1_profile_out.aac':
Duration: 00:00:14.58, bitrate: 39 kb/s
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 39 kb/s
3.3 文件格式
音频基础
从文章中可以知道AAC编码的文件扩展名主要有3种:aac、m4a、mp4。
示例
# m4a
ffmpeg -i in.wav -c:a libfdk_aac out.m4a
# mp4
ffmpeg -i in.wav -c:a libfdk_aac out.mp4
4.编程
- ffmpegs.h
#ifndef FFMPEGS_H
#define FFMPEGS_H
extern "C" {
#include
}
typedef struct {
const char *filename;
int sampleRate;
AVSampleFormat sampleFmt;
int chLayout;
} AudioEncodeSpec;
class FFmpegs
{
public:
FFmpegs();
static void aacEncode(AudioEncodeSpec &in,
const char *outFilename);
};
#endif // FFMPEGS_H
- ffmpegs.cpp
#include "ffmpegs.h"
#include
#include
extern "C" {
#include
#include
}
#define ERROR_BUF(ret) \
char errbuf[1024]; \
av_strerror(ret, errbuf, sizeof (errbuf));
FFmpegs::FFmpegs() {
}
// 检查采样格式
static int check_sample_fmt(const AVCodec *codec,
enum AVSampleFormat sample_fmt) {
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
// qDebug() << av_get_sample_fmt_name(*p);
if (*p == sample_fmt) return 1;
p++;
}
return 0;
}
// 音频编码
// 返回负数:中途出现了错误
// 返回0:编码操作正常完成
static int encode(AVCodecContext *ctx,
AVFrame *frame,
AVPacket *pkt,
QFile &outFile) {
// 发送数据到编码器
int ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_send_frame error" << errbuf;
return ret;
}
// 不断从编码器中取出编码后的数据
// while (ret >= 0)
while (true) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
// 继续读取数据到frame,然后送到编码器
return 0;
} else if (ret < 0) { // 其他错误
return ret;
}
// 成功从编码器拿到编码后的数据
// 将编码后的数据写入文件
outFile.write((char *) pkt->data, pkt->size);
// 释放pkt内部的资源
av_packet_unref(pkt);
}
}
void FFmpegs::aacEncode(AudioEncodeSpec &in,
const char *outFilename) {
// 文件
QFile inFile(in.filename);
QFile outFile(outFilename);
// 返回结果
int ret = 0;
// 编码器
AVCodec *codec = nullptr;
// 编码上下文
AVCodecContext *ctx = nullptr;
// 存放编码前的数据(pcm)
AVFrame *frame = nullptr;
// 存放编码后的数据(aac)
AVPacket *pkt = nullptr;
// 获取编码器
// codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
codec = avcodec_find_encoder_by_name("libfdk_aac");
if (!codec) {
qDebug() << "encoder not found";
return;
}
// libfdk_aac对输入数据的要求:采样格式必须是16位整数
// 检查输入数据的采样格式
if (!check_sample_fmt(codec, in.sampleFmt)) {
qDebug() << "unsupported sample format"
<< av_get_sample_fmt_name(in.sampleFmt);
return;
}
// 创建编码上下文
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error";
return;
}
// 设置PCM参数
ctx->sample_rate = in.sampleRate;
ctx->sample_fmt = in.sampleFmt;
ctx->channel_layout = in.chLayout;
// 比特率
ctx->bit_rate = 32000;
// 规格
ctx->profile = FF_PROFILE_AAC_HE_V2;
// 打开编码器
// AVDictionary *options = nullptr;
// av_dict_set(&options, "vbr", "5", 0);
// ret = avcodec_open2(ctx, codec, &options);
// avcodec_open2 error Invalid argument https://stackoverflow.com/questions/26205017/ffmpeg-avcodec-open2-returns-22-if-i-change-my-speaker-configuration/26205126
ret = avcodec_open2(ctx, codec, nullptr);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_open2 error" << errbuf;
goto end;
}
// 创建AVFrame
frame = av_frame_alloc();
if (!frame) {
qDebug() << "av_frame_alloc error";
goto end;
}
// frame缓冲区中的样本帧数量(由ctx->frame_size决定)
frame->nb_samples = ctx->frame_size;
frame->format = ctx->sample_fmt;
frame->channel_layout = ctx->channel_layout;
// 利用nb_samples、format、channel_layout创建缓冲区
ret = av_frame_get_buffer(frame, 0);
if (ret) {
ERROR_BUF(ret);
qDebug() << "av_frame_get_buffer error" << errbuf;
goto end;
}
// 创建AVPacket
pkt = av_packet_alloc();
if (!pkt) {
qDebug() << "av_packet_alloc error";
goto end;
}
// 打开文件
if (!inFile.open(QFile::ReadOnly)) {
qDebug() << "file open error" << in.filename;
goto end;
}
if (!outFile.open(QFile::WriteOnly)) {
qDebug() << "file open error" << outFilename;
goto end;
}
// 读取数据到frame中
while ((ret = inFile.read((char *) frame->data[0],
frame->linesize[0])) > 0) {
// 从文件中读取的数据,不足以填满frame缓冲区
if (ret < frame->linesize[0]) {
int bytes = av_get_bytes_per_sample((AVSampleFormat) frame->format);
int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
// 设置真正有效的样本帧数量
// 防止编码器编码了一些冗余数据
frame->nb_samples = ret / (bytes * ch);
}
// 进行编码
if (encode(ctx, frame, pkt, outFile) < 0) {
goto end;
}
}
// 刷新缓冲区
encode(ctx, nullptr, pkt, outFile);
end:
// 关闭文件
inFile.close();
outFile.close();
// 释放资源
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&ctx);
qDebug() << "线程正常结束";
}
- audiothread.h
#ifndef AUDIOTHREAD_H
#define AUDIOTHREAD_H
#include
class AudioThread : public QThread {
Q_OBJECT
private:
void run();
public:
explicit AudioThread(QObject *parent = nullptr);
~AudioThread();
signals:
};
#endif // AUDIOTHREAD_H
- audiothread.cpp
#include "audiothread.h"
#include
#include "ffmpegs.h"
AudioThread::AudioThread(QObject *parent) : QThread(parent)
{
//当监听到线程结束时候(finished),就调用deleteLater回收内存
connect(this,&AudioThread::finished,
this,&AudioThread::deleteLater);
}
AudioThread::~AudioThread(){
//断开所有的连接
disconnect();
//内存回收之前,正常结束线程
requestInterruption();
//安全退出
quit();
wait();
qDebug() << this << "析构(内存被回收)";
}
void AudioThread::run(){
AudioEncodeSpec in;
in.filename = "/Users/songlin/audio/resample/44100_s16le_2.pcm";
in.sampleRate = 44100;
in.sampleFmt = AV_SAMPLE_FMT_S16;
in.chLayout = AV_CH_LAYOUT_STEREO;
FFmpegs::aacEncode(in,"/Users/songlin/audio/aac_encode/out.aac");
}
5.参考链接
参考链接