年假在家研究一了一下流媒體,之前客戶需求要把mp4轉成h264+pcm 串流,紀錄一下
前言
libfaac 相信轉過aac格式的人都知道。他有一個對應的庫:libfaad,可用於將aac轉為pcm,windows , linux , macOS 都有offcial release,其他os 可能要交叉編譯就是了。
WAV 檔實際上就是 wav header (72 byte)+ pcm source,方便播放&debug 就faad decode pcm前面加上了
詳細介紹wav header.
FAAD (libfaad)的已知錯誤
非常坑爹
除非從原碼階段自行編譯,否則AAC mono一律會變成stereo,網路上已有先見詳述原因與解法
原碼
Makefile 要怎樣include faad
CC = gcc
LIB= -lfaad
INC = neaacdec.h faad.h
SRC =
SIMPLE_MAIN = aac_decode.c
MAIN = aac_decode.c
TARGET = aac_decoder
LDFLAG = -pthread
CFLAG = $(SRC) $(MAIN) $(INC) $(LDFLAG) $(LIB)
all:
$(CC) $(CFLAG) -o $(TARGET)
mv $(TARGET) ./bin
@echo "make all"
clean:
rm -f bin/*
@echo "clean all"
faad example
/*
* Example file that decodes raw AAC data from stdin,
* and outputs the PCM audio data to stdout.
*/
#include <neaacdec.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct WavFileHeader
{
char id[4]; // should always contain "RIFF"
int totallength; // total file length minus 8
char wavefmt[8]; // should be "WAVEfmt "
int format; // 16 for PCM format
short pcm; // 1 for PCM format
short channels; // channels
int frequency; // sampling frequency
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
char data[4]; // should always contain "data"
int bytes_in_data;
};
void write_wav_header(FILE* file, int totalsamcnt_per_channel, int samplerate, int channels){