音視頻同步原理及實現
可參考:
https://blog.csdn.net/myvest/article/details/97416415
https://www.jianshu.com/p/3578e794f6b5
總結的非常詳細。
ffmpeg 推流 音視頻同步推流
- 採用策略
將視頻同步到音頻上:以音頻的推送速度爲基準來同步視頻。 - 代碼實現
ffmpeg源碼中給出的實現:
// Write the stream header, if any.
ret = avformat_write_header(oc, &opt);
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file: %s\n",
av_err2str(ret));
return 1;
}
while (encode_video || encode_audio) {
/* select the stream to encode */
if (encode_video &&
(!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,
audio_st.next_pts, audio_st.enc->time_base) <= 0)) {
encode_video = !write_video_frame(oc, &video_st);
} else {
encode_audio = !write_audio_frame(oc, &audio_st);
}
}
我的代碼實現:
// 根據pts同步進行同步
if (audioSendPts == -2048 && videoSendPts == -1)
{
audio_stream_send(outs);
} else if (audioSendPts == 0 && videoSendPts == -1){
video_stream_send_only();
} else {
// 比較時間戳,確定發送音頻還是視頻
// LOGD("videoSendPts = %" PRId64", audioSendPts = %" PRId64"", videoSendPts, audioSendPts);
if (av_compare_ts(videoSendPts + 1, pVideoCodecCtx->time_base,
audioSendPts + 2048, pAudioCodecCtx->time_base) <= 0) {
// 視頻合流、封裝
video_stream_send_only();
LOGD("av_compare_ts vido");
} else {
// 音頻合流、封裝
audio_stream_send(outs);
LOGD("av_compare_ts audio-------");
}
}
遇到的大坑
從視頻源獲取的幀率與我設置的不同,偶爾會出現一秒內的幀率比設置的幀率多一幀的現象,導致同步策略不起作用。一直以爲是同步的策略存在問題,始終未懷疑到視頻源的幀率不正常,踏了個大坑。