最近研究了一下使用libmp4v2库解封装MP4文件,将MP4文件解封装成H264视频文件和AAC音频文件
libmp4v2源码下载:https://github.com/TechSmith/mp4v2
示例代码:
/*
使用libmp4v2库解封装出视频帧和音频帧
编译:
gcc -g mp4_unpack.c -o mp4_unpack -I./include -L./lib -lmp4v2
参考文献:
https://blog.csdn.net/jay100500/article/details/52955232
https://blog.csdn.net/andyhuabing/article/details/40983423
https://www.cnblogs.com/0error1warning/p/13755920.html
https://blog.nowcoder.net/n/e8f9c78aa2e0453aa2800cce162435c5
https://blog.csdn.net/liukun321/article/details/25337425
https://www.cnblogs.com/zhangxuan/p/8809245.html
*/
#include <stdio.h>
#include <mp4v2/mp4v2.h>
#include <stdlib.h>
#include <string.h>
#define FRAME_WRITE_FILE (1)
#define USE_PROVIDER (0)
#define ADTS_HEADER_SIZE7 (7)
#define ADTS_HEADER_SIZE9 (9)
#if defined(USE_PROVIDER) && (USE_PROVIDER != 0)
static void* my_open( const char* name, MP4FileMode mode)
{
FILE *fp = NULL;
const char* om;
if (name == NULL || name[0] == '\0')
{
printf("[%s:%d] name is NULL\n", __FUNCTION__, __LINE__);
return NULL;
}
switch(mode)
{
case FILEMODE_READ:
om = "rb";
break;
case FILEMODE_MODIFY:
om = "r+b";
break;
case FILEMODE_CREATE:
om = "w+b";
break;
case FILEMODE_UNDEFINED:
default:
om = "rb";
break;
}
fp = fopen(name, om);
if (fp == NULL)
{
printf("[%s:%d] file(%s) open failed!\n", __FUNCTION__, __LINE__, name);
}
return fp;
}
static int my_seek(void* handle, int64_t pos)
{
if (!handle)
{
printf("[%s:%d] handle is NULL\n", __FUNCTION__, __LINE__);
return 0;
}
int ret = fseeko((FILE*)handle, pos, SEEK_SET); // fseeko用于处理大文件
return (ret != 0);
}
static int my_read(void* handle, void* buffer, int64_t size, int64_t* nin, int64_t maxChunkSize)
{
if (!handle)
{
printf("[%s:%d] handle is NULL\n", __FUNCTION__, __LINE__);
return 1;
}
if (!buffer || size <= 0)
{
printf("[%s:%d] param is invalid\n", __FUNCTION__, __LINE__);
return 1;
}
if (fread(buffer, size, 1, (FILE*)handle) != 1)
{
return 1;
}
if (nin != NULL)
*nin = size;
return 0;
}
static int my_write(void* handle, const void* buffer, int64_t size, int64_t* nout, int64_t maxChunkSize)
{
if (!handle)
{
printf("[%s:%d] handle is NULL\n", __FUNCTION__, __LINE__);
return 1;
}
if (!buffer || size <= 0)
{
printf("[%s:%d] param is invalid\n", __FUNCTION__, __LINE__);
return 1;
}
if (fwrite( buffer, size, 1, (FILE*)handle ) != 1)
return 1;
if (nout != NULL)
*nout = size;
return 0;
}
static int my_close (void* handle)
{
if (!handle)
{
printf("[%s:%d] handle is NULL\n", __FUNCTION__, __LINE__);
return 0;
}
int ret = fclose((FILE*)handle);
return (ret != 0);
}
#endif
/*
获取h264视频流
*/
static int get_h264_stream(const char *out_file,
MP4FileHandle file_handle,
MP4TrackId video_track_id,
MP4SampleId total_frame,
uint8_t *sps,
uint32_t spslen,
uint8_t *pps,
uint32_t ppslen)
{
if (!file_handle)
{
printf("[%s:%d] param file_handle error\n", __FUNCTION__, __LINE__);
return -1;
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
if (out_file == NULL || out_file[0] == '\0')
{
printf("[%s:%d] param out_file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
FILE *fp = fopen(out_file, "wb");
if (fp == NULL)
{
printf("[%s:%d] fopen:%s failed\n", __FUNCTION__, __LINE__, out_file);
return -1;
}
#endif
uint8_t nal_header[] = {0x00, 0x00, 0x00, 0x01};
uint8_t* data = NULL;
uint32_t size = 0;
MP4Timestamp start_time;
MP4Duration duration;
MP4Duration render_offset;
bool is_sync_sample = 0; // 是否是关键帧
MP4SampleId sample_id = 0;
uint8_t* frame_data = NULL;
uint16_t vid_width = MP4GetTrackVideoWidth(file_handle, video_track_id);
uint16_t vid_height = MP4GetTrackVideoHeight(file_handle, video_track_id);
double vid_fps = MP4GetTrackVideoFrameRate(file_handle, video_track_id);
printf("[%s:%d] vid_width:%d, vid_height:%d, vid_fps:%f\n", __FUNCTION__, __LINE__, vid_width, vid_height, vid_fps);
while(sample_id < total_frame)
{
sample_id++; // sample id can't be zero
if (!MP4ReadSample(file_handle, video_track_id, sample_id, &data, &size, &start_time, &duration, &render_offset, &is_sync_sample))
{
printf("[%s:%d] MP4ReadSample failed\n", __FUNCTION__, __LINE__);
if (data != NULL)
{
free(data);
data = NULL;
}
continue;
}
frame_data = (uint8_t *)malloc(spslen + ppslen + size);
if (!frame_data)
{
printf("[%s:%d] out of memory\n", __FUNCTION__, __LINE__);
if (data != NULL)
{
free(data);
data = NULL;
}
continue;
}
uint8_t *pos = frame_data;
//IDR֡帧,写入sps和pps
if (is_sync_sample)
{
//printf("[%s:%d] sample_id:%lu, total_frame:%lu\n", __FUNCTION__, __LINE__, sample_id, total_frame);
memcpy(pos, sps, spslen);
pos += spslen;
memcpy(pos, pps, ppslen);
pos += ppslen;
}
//h264frame 标准的h264帧,前面几个字节就是frame的长度,需要替换为标准的h264 nal头
if (data != NULL && size > 4)
{
data[0] = 0x00;
data[1] = 0x00;
data[2] = 0x00;
data[3] = 0x01;
memcpy(pos, data, size);
pos += size;
}
/*
如果传入MP4ReadSample的视频pData是null
它内部就会new 一个内存
如果传入的是已知的内存区域,
则需要保证空间bigger than max frames size.
*/
if (data != NULL)
{
free(data);
data = NULL;
}
/*
收集sps pps 和 sample Data,拼接成一帧h264数据
*/
if (pos != frame_data)
{
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
fwrite(frame_data, (pos - frame_data), 1, fp);
#endif
// TODO:这里送入解码器解码
}
if (frame_data != NULL)
{
free(frame_data);
frame_data = NULL;
}
if (sample_id == total_frame)
printf("[%s:%d] video sample finished, sample_id:%lu, total_frame:%lu\n", __FUNCTION__, __LINE__, sample_id, total_frame);
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
fflush(fp);
fclose(fp);
#endif
return 0;
}
/*
填充atds头
参考
https://www.cnblogs.com/lidabo/p/7261558.html
https://blog.nowcoder.net/n/e8f9c78aa2e0453aa2800cce162435c5
Structure{
AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ)
Header consists of 7 or 9 bytes (without or with CRC).
}
Letter Length (bits) Description
A 12 syncword 0xFFF, all bits must be 1
B 1 MPEG Version: 0 for MPEG-4, 1 for MPEG-2
C 2 Layer: always 0
D 1 protection absent, Warning, set to 1 if there is no CRC and 0 if there is CRC
E 2 profile, the MPEG-4 Audio Object Type minus 1
F 4 MPEG-4 Sampling Frequency Index (15 is forbidden)
G 1 private stream, set to 0 when encoding, ignore when decoding
H 3 MPEG-4 Channel Configuration (in the case of 0, the channel configuration is sent via an inband PCE)
I 1 originality, set to 0 when encoding, ignore when decoding
J 1 home, set to 0 when encoding, ignore when decoding
K 1 copyrighted stream, set to 0 when encoding, ignore when decoding
L 1 copyright start, set to 0 when encoding, ignore when decoding
M 13 frame length, this value must include 7 or 9 bytes of header length: FrameLength =
(D == 1 ? 7 : 9) + size(AACFrame)
O 11 Buffer fullness
P 2 Number of AAC frames (RDBs) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame per ADTS frame
Q 16 CRC if protection absent is 0
Usage in MPEG-TS
ADTS packet must be a content of PES packet. Pack AAC data inside ADTS frame, than pack inside PES packet, then mux by TS packetizer.
Usage in Shoutcast
ADTS frames goes one by one in TCP stream. Look for syncword, parse header and look for next syncword after.
*/
int fill_adts_header(
uint8_t* header,
int head_size,
int packet_len,
int audio_rate_index,
int audio_channel_count,
uint8_t audio_type)
{
int ret = -1;
if (header == NULL)
{
printf("[%s:%d] header is NULL\n", __FUNCTION__, __LINE__);
return -1;
}
if (head_size != ADTS_HEADER_SIZE7 && head_size != (ADTS_HEADER_SIZE9))
{
printf("[%s:%d] param error, head_size:%d\n", __FUNCTION__, __LINE__, head_size);
return -1;
}
int toal_len = head_size + packet_len; // ADTS帧长度包括 ADTS头大小 + AAC帧长
//printf("[%s:%d] head_size:%d, packet_len:%d\n", __FUNCTION__, __LINE__, head_size, packet_len);
if (ADTS_HEADER_SIZE7 == head_size) // fill in ADTS data
{
header[0] = (uint8_t)0xFF;
header[1] = (uint8_t)0xF1;
header[2] = (uint8_t)(((audio_type - 1) << 6) + (audio_rate_index << 2) + (audio_channel_count >> 2));
header[3] = (uint8_t)(((audio_channel_count & 0x03) << 6) + (toal_len >> 11));
header[4] = (uint8_t)((toal_len & 0x07FF) >> 3);
header[5] = (uint8_t)(((toal_len & 0x07) << 5) + 0x1F);
header[6] = (uint8_t)0xFC;
ret = 0;
}
return ret;
}
/*
获取acc数据
total_frame 音频总帧数
audio_rate_index 音频采样率索引
audio_channel_count 音频声道数
audio_type 音频类型,例如MPEG-4 AAC LC的值为0x02
*/
static int get_aac_stream(
const char *out_file,
MP4FileHandle file_handle,
MP4TrackId audio_track_id,
MP4SampleId total_frame,
int audio_rate_index,
int audio_channel_count,
uint8_t audio_type)
{
if (!file_handle)
{
printf("[%s:%d] param file_handle error\n", __FUNCTION__, __LINE__);
return -1;
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
if (out_file == NULL || out_file[0] == '\0')
{
printf("[%s:%d] param out_file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
FILE *fp = fopen(out_file, "wb");
if (fp == NULL)
{
printf("[%s:%d] open file failed\n", __FUNCTION__, __LINE__);
return -1;
}
#endif
MP4SampleId frame_index = 0; //
uint8_t* data = NULL;
uint32_t size = 0;
MP4Timestamp start_time;
MP4Duration duration;
MP4Duration render_offset;
bool is_sync_sample = 0; // 是否是关键帧
while (frame_index < total_frame) // 获取acc数据帧
{
frame_index++; //MP4ReadSample函数的参数要求是从1开始
/*
如果传入MP4ReadSample的音频data是null,它内部就会new 一个内存
如果传入的是已知的内存区域,则需要保证空间bigger then max frames size.
*/
if (!MP4ReadSample(file_handle, audio_track_id, frame_index, &data, &size, &start_time, &duration, &render_offset, &is_sync_sample))
{
printf("[%s:%d] MP4ReadSample failed\n", __FUNCTION__, __LINE__);
if (data != NULL)
{
free(data);
data = NULL;
}
continue;
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
uint8_t header[ADTS_HEADER_SIZE7] = { 0 };
// 编码AAC裸流的时候,会遇到写出来的AAC文件并不能在PC和手机上播放,很大的可能就是AAC文件的每一帧里缺少了ADTS头信息文件的包装拼接
fill_adts_header(header, ADTS_HEADER_SIZE7, size, audio_rate_index, audio_channel_count, audio_type);
fwrite(header, ADTS_HEADER_SIZE7, 1, fp); // 先写入adts头
fwrite(data, size, 1, fp); // 再写入acc数据
#endif
// TODO:收集一帧acc数据,送入解码器
if (data)
{
free(data);
data = NULL;
}
if (frame_index == total_frame)
printf("[%s:%d] audio sample finished, frame_index:%lu, total_frame:%lu\n", __FUNCTION__, __LINE__, frame_index, total_frame);
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
if (fp != NULL)
{
fflush(fp);
fclose(fp);
}
#endif
return 0;
}
/*
解封装MP4文件成视频和音频
*/
static int unpack_mp4_file(const char* mp4_file, const char *video_file, const char *audio_file)
{
if (mp4_file == NULL || mp4_file[0] == '\0')
{
printf("[%s:%d] param mp4_file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
if (video_file == NULL || video_file[0] == '\0')
{
printf("[%s:%d] param video_file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
if (audio_file == NULL || audio_file[0] == '\0')
{
printf("[%s:%d] param audio_file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
#endif
MP4FileHandle file_handle = MP4_INVALID_FILE_HANDLE;
#if defined(USE_PROVIDER) && (USE_PROVIDER != 0)
MP4FileProvider provider;
provider.open = my_open;
provider.seek = my_seek;
provider.read = my_read;
provider.write = my_write;
provider.close = my_close;
file_handle = MP4ReadProvider(mp4_file, &provider);
#else
file_handle = MP4Read(mp4_file);
#endif
if (file_handle == MP4_INVALID_FILE_HANDLE)
{
printf("[%s:%d] MP4Read failed\n", __FUNCTION__, __LINE__);
return -1;
}
if (!MP4Dump(file_handle, 0))
{
printf("[%s:%d] MP4Dump failed\n", __FUNCTION__, __LINE__);
}
uint32_t track_count = MP4GetNumberOfTracks(file_handle, NULL, 0); // 获取所有的轨道数
printf("[%s:%d] track_count = %u\n", __FUNCTION__, __LINE__, track_count);
uint32_t file_time_scale = MP4GetTimeScale(file_handle); // 获取文件每秒刻度数
printf("[%s:%d] file_time_scale = %u\n", __FUNCTION__, __LINE__, file_time_scale);
MP4Duration file_duration = MP4GetDuration(file_handle); // 获取文件总刻度数
printf("[%s:%d] file_duration = %llu\n", __FUNCTION__, __LINE__, file_duration);
uint32_t msec = file_duration * 1000 / file_time_scale;// 总毫秒数 = 总刻度数*1000/每秒刻度数 => 先乘法可以降低误差
printf("[%s:%d] msec = %u\n\n", __FUNCTION__, __LINE__, msec);
MP4TrackId track_id = MP4_INVALID_TRACK_ID;
MP4TrackId track_id_vid = MP4_INVALID_TRACK_ID;
MP4TrackId tarck_id_aud = MP4_INVALID_TRACK_ID;
uint32_t track_index = 0;
MP4SampleId vid_samples_count = 0;
MP4SampleId aud_samples_count = 0;
uint32_t spslen = 0, ppslen = 0;
uint8_t *sps = NULL;
uint8_t *pps = NULL;
int audio_rate_index = -1; // 音频采样率索引
int audio_channel_count = 0; // 音频声道数
uint8_t audio_type = 0x00; // 音频类型,例如MPEG-4 AAC LC的值为0x02
for (track_index = 0; track_index < track_count; track_index++)
{
track_id = MP4FindTrackId(file_handle, track_index, NULL, 0);
printf("[%s:%d] track_index:%u, track_id:%u\n", __FUNCTION__, __LINE__, track_index, track_id);
const char* type = MP4GetTrackType(file_handle, track_id);
printf("[%s:%d] type:%s\n", __FUNCTION__, __LINE__, type);
if (MP4_IS_VIDEO_TRACK_TYPE(type)) // 视频类型
{
const char* track_name = MP4GetTrackMediaDataName(file_handle, track_id);
printf("[%s:%d] track_name:%s\n", __FUNCTION__, __LINE__, track_name);
uint32_t time_scale = MP4GetTrackTimeScale(file_handle, track_id);
printf("[%s:%d] time_scale:%u\n", __FUNCTION__, __LINE__, time_scale);
//MP4Duration duration = MP4GetTrackDuration(file_handle, track_id);
//printf("[%s:%d] duration:%llu\n", __FUNCTION__, __LINE__, duration);
vid_samples_count = MP4GetTrackNumberOfSamples(file_handle, track_id);
printf("[%s:%d] vid_samples_count:%u\n", __FUNCTION__, __LINE__, vid_samples_count);
uint32_t bit_rate = MP4GetTrackBitRate(file_handle, track_id); // 比特率
printf("[%s:%d] bit_rate:%u\n", __FUNCTION__, __LINE__, bit_rate);
double video_frame_rate = MP4GetTrackVideoFrameRate(file_handle, track_id);
printf("[%s:%d] video_frame_rate(fps):%f\n", __FUNCTION__, __LINE__, video_frame_rate);
uint16_t vid_width = MP4GetTrackVideoWidth(file_handle, track_id);
uint16_t vid_height = MP4GetTrackVideoHeight(file_handle, track_id);
printf("[%s:%d] vid_width:%d, vid_height:%d\n", __FUNCTION__, __LINE__, vid_width, vid_height);
uint32_t max_video = MP4GetTrackMaxSampleSize(file_handle, track_id);
printf("[%s:%d] max_video:%u\n", __FUNCTION__, __LINE__, max_video);
char* info = MP4Info(file_handle, track_id);
if (info != NULL)
{
printf("[%s:%d] info: %s\n", __FUNCTION__, __LINE__, info);
free(info);
}
track_id_vid = track_id;
uint8_t **seq_header;
uint8_t **pict_header;
uint32_t *pict_header_size;
uint32_t *seq_header_size;
uint32_t index = 0;
uint32_t size = 0;
uint8_t nal_header[] = {0x00, 0x00, 0x00, 0x01};
/*
获取sps和pps
*/
if (!MP4GetTrackH264SeqPictHeaders(file_handle, track_id, &seq_header, &seq_header_size, &pict_header, &pict_header_size))
{
printf("[%s:%d] MP4GetTrackH264SeqPictHeaders failed\n", __FUNCTION__, __LINE__);
continue;
}
// 统计sps的长度
size = 4; // 加上4个字节的nal头
for (index = 0; seq_header_size[index] != 0; index++)
{
size += seq_header_size[index];
printf("[%s:%d] size = %d, seq_header_size[%d] = %d\n", __FUNCTION__, __LINE__, size, index, seq_header_size[index]);
}
printf("[%s:%d] sps with nal size:%u\n", __FUNCTION__, __LINE__, size);
sps = (uint8_t *)malloc(size);
if (sps == NULL)
{
MP4Free(seq_header);
MP4Free(seq_header_size);
MP4Free(pict_header);
MP4Free(pict_header_size);
goto end;
}
uint8_t *sps_base = sps;
memcpy(sps_base, nal_header, 4);
sps_base += 4;
for (index = 0; seq_header_size[index] != 0; index++)
{
memcpy(sps_base, seq_header[index], seq_header_size[index]);
sps_base += seq_header_size[index];
MP4Free(seq_header[index]);
}
MP4Free(seq_header);
MP4Free(seq_header_size);
spslen = size;
// 统计pps的长度
size = 4; //加上4个字节的nal头
for (index = 0; pict_header_size[index] != 0; index++)
{
size += pict_header_size[index];
printf("[%s:%d] size = %u, pict_header_size[%d] = %u\n", __FUNCTION__, __LINE__, size, index, pict_header_size[index]);
}
printf("[%s:%d] pps with nal size:%lu\n", __FUNCTION__, __LINE__, size);
pps = (uint8_t *)malloc(size);
if (pps == NULL)
{
MP4Free(seq_header);
MP4Free(seq_header_size);
MP4Free(pict_header);
MP4Free(pict_header_size);
goto end;
}
uint8_t *pps_base = pps;
memcpy(pps_base, nal_header, 4);
pps_base += 4;
for (index = 0; pict_header_size[index] != 0; index++)
{
memcpy(pps_base, pict_header[index], pict_header_size[index]);
pps_base += pict_header_size[index];
MP4Free(pict_header[index]);
}
MP4Free(pict_header);
MP4Free(pict_header_size);
ppslen = size;
printf("[%s:%d] spslen(with nal):%u, ppslen(with nal):%u\n\n", __FUNCTION__, __LINE__, spslen, ppslen);
}
else if (MP4_IS_AUDIO_TRACK_TYPE(type)) // 音频类型
{
const char* track_name = MP4GetTrackMediaDataName(file_handle, track_id);
printf("[%s:%d] track_name:%s\n", __FUNCTION__, __LINE__, track_name);
uint32_t time_scale = MP4GetTrackTimeScale(file_handle, track_id); // 采样率
printf("[%s:%d] time_scale:%u\n", __FUNCTION__, __LINE__, time_scale);
//MP4Duration duration = MP4GetTrackDuration(file_handle, track_id);
//printf("[%s:%d] duration:%llu\n", __FUNCTION__, __LINE__, duration);
uint32_t bit_rate = MP4GetTrackBitRate(file_handle, track_id); // 比特率
printf("[%s:%d] bit_rate:%u\n", __FUNCTION__, __LINE__, bit_rate);
aud_samples_count = MP4GetTrackNumberOfSamples(file_handle, track_id); // 音频总帧数
printf("[%s:%d] aud_samples_count:%u\n", __FUNCTION__, __LINE__, aud_samples_count);
audio_channel_count = MP4GetTrackAudioChannels(file_handle, track_id); // 声道数
printf("[%s:%d] channel_count:%d\n", __FUNCTION__, __LINE__, audio_channel_count);
audio_type = MP4GetTrackAudioMpeg4Type(file_handle, track_id); // 音频类型
printf("[%s:%d] audio_type:0x%02X\n", __FUNCTION__, __LINE__, audio_type);
uint8_t audio_profile_level = MP4GetAudioProfileLevel(file_handle);
printf("[%s:%d] audio_profile_level:0x%02X\n", __FUNCTION__, __LINE__, audio_profile_level);
uint32_t max_audio = MP4GetTrackMaxSampleSize(file_handle, track_id);
printf("[%s:%d] max_audio:%u\n", __FUNCTION__, __LINE__, max_audio);
char* info = MP4Info(file_handle, track_id);
if (info != NULL)
{
printf("[%s:%d] info: %s\n", __FUNCTION__, __LINE__, info);
free(info);
}
// 采样率与下标的关系, 参考 ffmpeg mpeg4audio.c int avpriv_mpeg4audio_sample_rates[16]
uint32_t audio_sample_rates[] = {
96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350
};
int i = 0;
for (i = 0; i < sizeof(audio_sample_rates)/sizeof(int); i++)
{
if (time_scale == audio_sample_rates[i])
{
audio_rate_index = i; // 例如: audio_time_scale == 48000 hz, audio_rate_index = 3
break;
}
}
if (audio_rate_index == -1)
{
printf("[%s:%d] audio sample rate %d is not supported, audio_rate_index = %d\n", __FUNCTION__, __LINE__, time_scale, audio_rate_index);
goto end;
}
tarck_id_aud = track_id;
}
else
{
printf("[%s:%d] unknow track type:%s\n", __FUNCTION__, __LINE__, type);
}
}
// 解析视频帧
if (track_id_vid > 0)
{
get_h264_stream(video_file, file_handle, track_id_vid, vid_samples_count, sps, spslen, pps, ppslen);
}
// 解析音频帧
if (tarck_id_aud > 0)
{
get_aac_stream(audio_file, file_handle, tarck_id_aud, aud_samples_count, audio_rate_index, audio_channel_count, audio_type);
}
end:
if (sps)
{
free(sps);
sps = NULL;
}
if (pps)
{
free(pps);
pps = NULL;
}
if (file_handle)
{
MP4Close(file_handle, 0);
file_handle = MP4_INVALID_FILE_HANDLE;
}
return 0;
}
int main(int argc, char*argv[])
{
char * video_file = NULL;
char * audio_file = NULL;
#if defined(FRAME_WRITE_FILE) && (FRAME_WRITE_FILE != 0)
if (argc != 4)
{
printf("[%s:%d] usage: %s file.mp4 out_video.h264 out_audio.acc\n", __FUNCTION__, __LINE__, argv[0]);
return -1;
}
#endif
char * file_name = argv[1];
video_file = argv[2];
audio_file = argv[3];
unpack_mp4_file(file_name, video_file, audio_file);
return 0;
}