音视频同步-ffmpeg-推流
目录
音视频同步 ffmpeg 推流
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-------");
}
}
**本文福利,
C++音视频学习资料包、技术视频** ,内容包括(音视频开发,面试题, FFmpeg , webRTC , rtmp , hls , rtsp , ffplay , srs **)
↓↓↓↓↓↓
见下面↓↓文章底部点击领取↓↓**