参考://www.greatytc.com/p/a346f93ddaff
本人懒 从OC方法倒着复制的 建议倒着看
IJKMediaPlayback.h
- (void)stopRecord;
- (void)startRecordWithFileName:(NSString *)fileName;
IJKFFMoviePlayerController.m
- (void)stopRecord {
ijkmp_stop_recording(_mediaPlayer);
}
- (void)startRecordWithFileName:(NSString *)fileName {
// 视频存储的路径
const char *path = [fileName cStringUsingEncoding:NSUTF8StringEncoding];
ijkmp_start_recording (_mediaPlayer, path);
}
- (BOOL)isRecording {
return ijkmp_isRecording(_mediaPlayer);
}
ff_ffplay_def.h
/* ffplayer */
struct IjkMediaMeta;
struct IJKFF_Pipeline;
typedef struct FFPlayer {
...
AVFormatContext *m_ofmt_ctx; // 用于输出的AVFormatContext结构体
AVOutputFormat *m_ofmt;
pthread_mutex_t record_mutex; // 锁
int is_record; // 是否在录制
int record_error;
int is_first; // 第一帧数据
int64_t start_v_pts; // 开始录制时pts 视频
int64_t start_v_dts; // 开始录制时dts 视频
int64_t start_a_pts; // 开始录制时pts 音频
int64_t start_a_dts; // 开始录制时dts 音频
} FFPlayer;
ijkplayer.c
static int ijkmp_start_recording_l(IjkMediaPlayer *mp,const char *filePath)
{
// av_log(mp->ffplayer,AV_LOG_WARING,"cjz ijkmp_start_recording_l filePath %s",filePath);
return ffp_start_recording_l(mp->ffplayer,filePath);
}
int ijkmp_start_recording(IjkMediaPlayer *mp,const char *filePath)
{
assert(mp);
pthread_mutex_lock(&mp->mutex);
av_log(mp->ffplayer,AV_LOG_WARNING,"cjz ijkmp_start_recording");
int retval = ijkmp_start_recording_l(mp,filePath);
pthread_mutex_unlock(&mp->mutex);
return retval;
}
static int ijkmp_stop_recording_l(IjkMediaPlayer *mp)
{
return ffp_stop_recording_l(mp->ffplayer);
}
int ijkmp_stop_recording(IjkMediaPlayer *mp)
{
assert(mp);
pthread_mutex_lock(&mp->mutex);
av_log(mp->ffplayer,AV_LOG_WARNING,"cjz ijkmp_stop_recording");
int retval = ijkmp_stop_recording_l(mp);
pthread_mutex_unlock(&mp->mutex);
return retval;
}
static int ijkmp_isRecordFinished_l(IjkMediaPlayer *mp)
{
return ffp_record_isfinished_l(mp->ffplayer);
}
int ijkmp_isRecordFinished(IjkMediaPlayer *mp)
{
assert(mp);
pthread_mutex_lock(&mp->mutex);
av_log(mp->ffplayer,AV_LOG_WARNING,"cjz ijkmp_isRecordFinished ");
int retval = ijkmp_isRecordFinished_l(mp);
pthread_mutex_unlock(&mp->mutex);
return retval;
}
int ijkmp_isRecording(IjkMediaPlayer *mp) {
return mp->ffplayer->is_record;
}
ff_ffplay.h
int ffp_start_recording_l(FFPlayer *ffp,const char *file_name);
int ffp_record_isfinished_l(FFPlayer *ffp);
int ffp_stop_recording_l(FFPlayer *ffp);
ff_ffplay.c
//插入录制方法
int ffp_start_recording_l(FFPlayer *ffp,const char *file_name)
{
assert(ffp);
VideoState *is = ffp->is;
ffp->m_ofmt_ctx = NULL;
ffp->m_ofmt = NULL;
ffp->is_record = 0;
ffp->record_error = 0;
if (!file_name || !strlen(file_name)) { // 没有路径
av_log(ffp, AV_LOG_ERROR, "filename is invalid");
goto end;
}
if (!is || !is->ic|| is->paused || is->abort_request) { // 没有上下文,或者上下文已经停止
av_log(ffp, AV_LOG_ERROR, "is,is->ic,is->paused is invalid");
goto end;
}
if (ffp->is_record) { // 已经在录制
av_log(ffp, AV_LOG_ERROR, "recording has started");
goto end;
}
// 初始化一个用于输出的AVFormatContext结构体
avformat_alloc_output_context2(&ffp->m_ofmt_ctx, NULL, NULL, file_name);
if (!ffp->m_ofmt_ctx) {
av_log(ffp, AV_LOG_ERROR, "Could not create output context filename is %s\n", file_name);
goto end;
}
ffp->m_ofmt = ffp->m_ofmt_ctx->oformat;
for (int i = 0; i < is->ic->nb_streams; i++) {
// 对照输入流创建输出流通道
AVStream *in_stream = is->ic->streams[i];
AVStream *out_stream = avformat_new_stream(ffp->m_ofmt_ctx, in_stream->codec->codec);
if (!out_stream) {
av_log(ffp, AV_LOG_ERROR, "Failed allocating output stream\n");
goto end;
}
// 将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体
av_log(ffp, AV_LOG_DEBUG, "in_stream->codec;%p\n", in_stream->codec);
if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {
av_log(ffp, AV_LOG_ERROR, "Failed to copy context from input to output stream codec context\n");
goto end;
}
out_stream->codec->codec_tag = 0;
if (ffp->m_ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
}
av_dump_format(ffp->m_ofmt_ctx, 0, file_name, 1);
// 打开输出文件
if (!(ffp->m_ofmt->flags & AVFMT_NOFILE)) {
if (avio_open(&ffp->m_ofmt_ctx->pb, file_name, AVIO_FLAG_WRITE) < 0) {
av_log(ffp, AV_LOG_ERROR, "Could not open output file '%s'", file_name);
goto end;
}
}
// 写视频文件头
if (avformat_write_header(ffp->m_ofmt_ctx, NULL) < 0) {
av_log(ffp, AV_LOG_ERROR, "Error occurred when opening output file\n");
goto end;
}
ffp->is_record = 1;
ffp->record_error = 0;
pthread_mutex_init(&ffp->record_mutex, NULL);
return 0;
end:
ffp->record_error = 1;
return -1;
}
int ffp_record_isfinished_l(FFPlayer *ffp)
{
return 0;
}
int ffp_record_file(FFPlayer *ffp, AVPacket *packet)
{
assert(ffp);
VideoState *is = ffp->is;
int ret = 0;
AVStream *in_stream;
AVStream *out_stream;
if (ffp->is_record) {
if (packet == NULL) {
ffp->record_error = 1;
av_log(ffp, AV_LOG_ERROR, "packet == NULL");
return -1;
}
AVPacket *pkt = (AVPacket *)av_malloc(sizeof(AVPacket)); // 与看直播的 AVPacket分开,不然卡屏
av_new_packet(pkt, 0);
if (0 == av_packet_ref(pkt, packet)) {
pthread_mutex_lock(&ffp->record_mutex);
if (!ffp->is_first) { // 录制的第一帧,时间从0开始
ffp->is_first = 1;
pkt->pts = 0;
pkt->dts = 0;
} else { // 之后的每一帧都要减去,点击开始录制时的值,这样的时间才是正确的
if (pkt->stream_index == AVMEDIA_TYPE_AUDIO) {
pkt->pts = llabs(pkt->pts - ffp->start_a_pts);
pkt->dts = llabs(pkt->dts - ffp->start_a_dts);
}
else if (pkt->stream_index == AVMEDIA_TYPE_VIDEO) {
pkt->pts = pkt->dts = llabs(pkt->dts - ffp->start_v_dts);
}
}
in_stream = is->ic->streams[pkt->stream_index];
out_stream = ffp->m_ofmt_ctx->streams[pkt->stream_index];
// 转换PTS/DTS
pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt->duration = av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base);
pkt->pos = -1;
// 写入一个AVPacket到输出文件
if ((ret = av_interleaved_write_frame(ffp->m_ofmt_ctx, pkt)) < 0) {
av_log(ffp, AV_LOG_ERROR, "Error muxing packet\n");
}
av_packet_unref(pkt);
pthread_mutex_unlock(&ffp->record_mutex);
} else {
av_log(ffp, AV_LOG_ERROR, "av_packet_ref == NULL");
}
}
return ret;
}
int ffp_stop_recording_l(FFPlayer *ffp){
assert(ffp);
if (ffp->is_record) {
ffp->is_record = 0;
pthread_mutex_lock(&ffp->record_mutex);
if (ffp->m_ofmt_ctx != NULL) {
av_write_trailer(ffp->m_ofmt_ctx);
if (ffp->m_ofmt_ctx && !(ffp->m_ofmt->flags & AVFMT_NOFILE)) {
avio_close(ffp->m_ofmt_ctx->pb);
}
avformat_free_context(ffp->m_ofmt_ctx);
ffp->m_ofmt_ctx = NULL;
ffp->is_first = 0;
}
pthread_mutex_unlock(&ffp->record_mutex);
pthread_mutex_destroy(&ffp->record_mutex);
av_log(ffp, AV_LOG_DEBUG, "stopRecord ok\n");
} else {
av_log(ffp, AV_LOG_ERROR, "don't need stopRecord\n");
}
return 0;
}
// ff_ffplay.c 自带方法
static int read_thread(void *arg) {
...
for (;;) {
//插入
if (!ffp->is_first && pkt->pts == pkt->dts) { // 获取开始录制前dts等于pts最后的值,用于
if (pkt->stream_index == AVMEDIA_TYPE_AUDIO) {
ffp->start_a_pts = pkt->pts;
ffp->start_a_dts = pkt->dts;
}
}
if (pkt->stream_index == AVMEDIA_TYPE_VIDEO) {
if (!ffp->is_first) {
ffp->start_v_pts = pkt->pts;
ffp->start_v_dts = pkt->dts;
// printf("set start video start_pts: %lld +++++ start_dts: %lld\n", ffp->start_v_pts, ffp->start_v_dts);
}
}
if (pkt->stream_index == AVMEDIA_TYPE_VIDEO) {
// printf("video pts: %lld +++++ dts: %lld\n", pkt->pts, pkt->dts);
}
#pragma mark - 录制插入
if (ffp->is_record) { // 可以录制时,写入文件
if (0 != ffp_record_file(ffp, pkt)) {
ffp->record_error = 1;
ffp_stop_recording_l(ffp);
}
}
...
}
...
}