5127
- 收藏
- 点赞
- 分享
- 举报
mp4v2 录制音视频,视频播放速度慢
我用 mp4v2 录制音视频,录下来的 mp4 文件用暴风影音播放,发现音频是正常播放的,但是视频播放的速度比较慢,可以从录制视频右上方显示的时间看出来,当音频播放完毕后,就是没有声音了,只有图像,这时候视频才开始正常播放,导致音视频不同步,因为刚开始视频播放慢的原因。请问一下,这是什么原因?什么参数设置错了?谢谢!
我的代码:
#define VIDEO_TIME_SCALE 90000
#define FRAMES_PER_SECOND 25 // frame rate
#define BUFFER_SIZE (1024 * 1024)
#define AUDIO_TIME_SCALE 44100
MP4FileHandle file = MP4_INVALID_FILE_HANDLE;
MP4TrackId audio = MP4_INVALID_TRACK_ID;
MP4TrackId video = MP4_INVALID_TRACK_ID;
char isFirstSPS = 1;
char isFirstPPS = 1;
int mp4_init(void)
{
unsigned char data[2] = {0x12, 0x8};
// 创建 mp4 文件 handler
file = MP4Create("test_aac.mp4", 0);
if (file == MP4_INVALID_FILE_HANDLE)
{
printf("create and open MP4 file failed!\n");
return 0;
}
/* 添加音频 track */
audio = MP4AddAudioTrack(file, AUDIO_TIME_SCALE, 320, MP4_MPEG4_AUDIO_TYPE);
if (audio == MP4_INVALID_TRACK_ID)
{
printf("Add audio track failed!\n");
return 0;
}
// 设置音频属性
MP4SetAudioProfileLevel(file, 0x02);
// unsigned char data[2] = {0x12, 0x8};
MP4SetTrackESConfiguration(file, audio, data, 2);
return 1;
}
void writeAACtoMP4(unsigned char *aac_buf, int len)
{
MP4WriteSample(file, audio, aac_buf + 7, len - 7, 320, 0, 1);
}
int writeH264toMP4(unsigned char *buffer, unsigned int frame_size)
{
char nalu_type = buffer[4] & 0x1f;
unsigned char *nalu_data = (unsigned char *) &buffer[4];
unsigned int nalu_size = frame_size - 4;
if(nalu_type == 0x07 && 1 == isFirstSPS) // sps
{
// 添加 h264 track
/* VIDEO_TIME_SCALE = 90000 FRAME_PESR_SECOND = 25 */
video = MP4AddH264VideoTrack
( file,
VIDEO_TIME_SCALE,
VIDEO_TIME_SCALE / FRAME_PESR_SECOND,
1080, // width
720, // height
nalu_data[1], // sps[1] AVCProfileIndication
nalu_data[2], // sps[2] profile_compat
nalu_data[3], // sps[3] AVCLevelIndication
3); // 4 bytes length before each NAL unit
if (video == MP4_INVALID_TRACK_ID)
{
printf("add video track failed.\n");
return 0;
}
MP4SetVideoProfileLevel(file, 0x7f); // Simple Profile @ Level 3
MP4AddH264SequenceParameterSet(file, video, nalu_data, nalu_size);
isFirstSPS = 0;
}
else if(nalu_type == 0x08 && 1 == isFirstPPS) // pps
{
MP4AddH264PictureParameterSet(file, video, nalu_data, nalu_size);
isFirstPPS = 0;
}
else
{
// MP4 Nalu 前四个字节表示 Nalu 长度
buffer[0] = nalu_size >> 24;
buffer[1] = nalu_size >> 16;
buffer[2] = nalu_size >> 8;
buffer[3] = nalu_size & 0xff;
if(!MP4WriteSample(file, video, buffer, frame_size, 3600, 0, 1))
{
printf("MP4WriteSample failed\n");
return 0;
}
}
return 0;
}
我的代码:
#define VIDEO_TIME_SCALE 90000
#define FRAMES_PER_SECOND 25 // frame rate
#define BUFFER_SIZE (1024 * 1024)
#define AUDIO_TIME_SCALE 44100
MP4FileHandle file = MP4_INVALID_FILE_HANDLE;
MP4TrackId audio = MP4_INVALID_TRACK_ID;
MP4TrackId video = MP4_INVALID_TRACK_ID;
char isFirstSPS = 1;
char isFirstPPS = 1;
int mp4_init(void)
{
unsigned char data[2] = {0x12, 0x8};
// 创建 mp4 文件 handler
file = MP4Create("test_aac.mp4", 0);
if (file == MP4_INVALID_FILE_HANDLE)
{
printf("create and open MP4 file failed!\n");
return 0;
}
/* 添加音频 track */
audio = MP4AddAudioTrack(file, AUDIO_TIME_SCALE, 320, MP4_MPEG4_AUDIO_TYPE);
if (audio == MP4_INVALID_TRACK_ID)
{
printf("Add audio track failed!\n");
return 0;
}
// 设置音频属性
MP4SetAudioProfileLevel(file, 0x02);
// unsigned char data[2] = {0x12, 0x8};
MP4SetTrackESConfiguration(file, audio, data, 2);
return 1;
}
void writeAACtoMP4(unsigned char *aac_buf, int len)
{
MP4WriteSample(file, audio, aac_buf + 7, len - 7, 320, 0, 1);
}
int writeH264toMP4(unsigned char *buffer, unsigned int frame_size)
{
char nalu_type = buffer[4] & 0x1f;
unsigned char *nalu_data = (unsigned char *) &buffer[4];
unsigned int nalu_size = frame_size - 4;
if(nalu_type == 0x07 && 1 == isFirstSPS) // sps
{
// 添加 h264 track
/* VIDEO_TIME_SCALE = 90000 FRAME_PESR_SECOND = 25 */
video = MP4AddH264VideoTrack
( file,
VIDEO_TIME_SCALE,
VIDEO_TIME_SCALE / FRAME_PESR_SECOND,
1080, // width
720, // height
nalu_data[1], // sps[1] AVCProfileIndication
nalu_data[2], // sps[2] profile_compat
nalu_data[3], // sps[3] AVCLevelIndication
3); // 4 bytes length before each NAL unit
if (video == MP4_INVALID_TRACK_ID)
{
printf("add video track failed.\n");
return 0;
}
MP4SetVideoProfileLevel(file, 0x7f); // Simple Profile @ Level 3
MP4AddH264SequenceParameterSet(file, video, nalu_data, nalu_size);
isFirstSPS = 0;
}
else if(nalu_type == 0x08 && 1 == isFirstPPS) // pps
{
MP4AddH264PictureParameterSet(file, video, nalu_data, nalu_size);
isFirstPPS = 0;
}
else
{
// MP4 Nalu 前四个字节表示 Nalu 长度
buffer[0] = nalu_size >> 24;
buffer[1] = nalu_size >> 16;
buffer[2] = nalu_size >> 8;
buffer[3] = nalu_size & 0xff;
if(!MP4WriteSample(file, video, buffer, frame_size, 3600, 0, 1))
{
printf("MP4WriteSample failed\n");
return 0;
}
}
return 0;
}
我来回答
回答12个
时间排序
认可量排序
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2016-12-26 21:18:32
-
2016-07-08 14:26:05
-
2018-06-21 16:23:19
-
2016-11-09 20:43:35
-
2019-12-06 14:20:53
-
2017-02-23 10:10:53
-
12019-05-20 14:20:24
-
2017-06-13 14:50:25
-
02015-05-11 13:38:56
-
42019-10-10 22:37:03
-
2016-05-20 17:29:59
-
2015-08-12 15:22:22
-
12021-06-18 10:42:16
-
2016-02-20 20:10:27
-
22014-06-30 17:26:38
-
2016-08-02 17:38:22
-
2015-06-29 16:32:19
-
2017-02-23 10:16:53
-
2017-06-08 17:41:13
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认