iOS ijkplayer添加录像、rtsp、https、编译、集成framework及简单使用

iOS ijkplayer添加录像、rtsp、https、编译、集成framework及简单使用,第1张

1,GitHub上clone ijkplayer源码,切换分支到0.8.8
$ git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
//进入ijkplayer-ios
$ cd ijkplayer-ios
//切换分支
$ git checkout -B latest k0.8.8
2,添加RTSP和HTTPS支持

开启支持RTSP,默认不支持RTSP,需要修改module-lite.sh内容,新增对应的协议,module-lite.sh是在config目录下

//目录:~/config/module-lite.sh 将这一行:
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=rtp"
//修改为:
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=rtp"
//新加:
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=rtsp"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=tcp"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-decoder=mjpeg"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=mjpeg"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-openssl"

然后修改支持module-lite.sh

//进入ijkplayer/config 目录
$ cd config
//移除module.sh文件
$ rm module.sh
//替换模块
$ ln -s module-lite.sh module.sh
3,修改代码

~/ijkmedia/ijkplayer/ff_ffplay.h

//添加:
//录制相关
int       ffp_start_record(FFPlayer *ffp, const char *file_name);
int       ffp_stop_record(FFPlayer *ffp);
int       ffp_record_file(FFPlayer *ffp, AVPacket *packet);

~/ijkmedia/ijkplayer/ff_ffplay.c

//添加:
//开始录制函数:file_name是保存路径
int ffp_start_record(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 *out_stream;
        AVStream *in_stream = is->ic->streams[i];
        AVCodecParameters *in_codecpar = in_stream->codecpar;
        
        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
            continue;
        }
        
        out_stream = avformat_new_stream(ffp->m_ofmt_ctx, NULL);
        if (!out_stream) {
            av_log(ffp, AV_LOG_ERROR, "Failed allocating output stream\n");
            goto end;
        }
        
        // 将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体
        if (avcodec_parameters_copy(out_stream->codecpar, in_codecpar) < 0) {
            av_log(ffp, AV_LOG_ERROR, "Failed to copy codec parameters\n");
            goto end;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    
    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_stop_record(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;
}


//保存文件
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 { // 之后的每一帧都要减去,点击开始录制时的值,这样的时间才是正确的
                pkt->pts = abs(pkt->pts - ffp->start_pts);
                pkt->dts = abs(pkt->dts - ffp->start_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;
}

//添加:
//read_thread方法中for (;;) 内3619行
···
ffp_check_buffering_l(ffp);
                }
            }
        }
        //开始新加
            if (!ffp->is_first && pkt->pts == pkt->dts) { // 获取开始录制前dts等于pts最后的值,用于
                ffp->start_pts = pkt->pts;
                ffp->start_dts = pkt->dts;
            }
            if (ffp->is_record) { // 可以录制时,写入文件
                if (0 != ffp_record_file(ffp, pkt)) {
                    ffp->record_error = 1;
                    ffp_stop_record(ffp);
                }
            }
            //结束新加
    }

    ret = 0;
 fail:
    if (ic && !is->ic)
···

~/ijkmedia/ijkplayer/ff_ffplay_def.h

//添加:
//FFPlayer内722行
typedef struct FFPlayer {
    ···
    int render_wait_start;
    
    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_pts;                  // 开始录制时pts
    int64_t start_dts;                  // 开始录制时dts
    
} FFPlayer;

~/ijkmedia/ijkplayer/ijkplayer.h

//添加:
int       ijkmp_start_record(IjkMediaPlayer *mp,const char *file_name);
int       ijkmp_stop_record(IjkMediaPlayer *mp);
int       ijkmp_isRecording(IjkMediaPlayer *mp);

~/ijkmedia/ijkplayer/ijkplayer.c

//添加:
int ijkmp_start_record(IjkMediaPlayer *mp,const char *file_name)
{
    assert(mp);
    MPTRACE("ijkmp_startRecord()\n");
    pthread_mutex_lock(&mp->mutex);
    int retval = ffp_start_record(mp->ffplayer,file_name);
    pthread_mutex_unlock(&mp->mutex);
    MPTRACE("ijkmp_startRecord()=%d\n", retval);
    return retval;
}

int ijkmp_stop_record(IjkMediaPlayer *mp)
{
    assert(mp);
    MPTRACE("ijkmp_stopRecord()\n");
    pthread_mutex_lock(&mp->mutex);
    int retval = ffp_stop_record(mp->ffplayer);
    pthread_mutex_unlock(&mp->mutex);
    MPTRACE("ijkmp_stopRecord()=%d\n", retval);
    return retval;
}
int ijkmp_isRecording(IjkMediaPlayer *mp) {
    return mp->ffplayer->is_record;
}

~/ios/IJKMediaPlayer/IJKMediaPlayer/IJKMediaPlayback.h

//添加:
- (void)stopRecord;
- (void)startRecordWithFileName:(NSString *)fileName;
- (BOOL)isRecording;

~/ios/IJKMediaPlayer/IJKMediaPlayer/IJKFFMoviePlayerController.m

//添加:
#pragma mark  --录像
- (void)stopRecord{
    ijkmp_stop_record(_mediaPlayer);
    NSLog(@"stop record");
}
- (void)startRecordWithFileName:(NSString *)fileName{
    // 视频存储的路径
    const char *path = [fileName cStringUsingEncoding:NSUTF8StringEncoding];
    ijkmp_start_record(_mediaPlayer, path);
    
    NSLog(@"start record fileName %@",fileName);
}
- (BOOL)isRecording {
    return ijkmp_isRecording(_mediaPlayer);
}

~/ios/IJKMediaPlayer/IJKMediaPlayer/IJKFFOptions.m

//添加:
+ (IJKFFOptions *)optionsByDefault
{
    ···
    [options setFormatOptionValue:@"ijkplayer"          forKey:@"user-agent"];
  //使用tcp
    [options setFormatOptionValue:@"tcp"                forKey:@"rtsp_transport"];

    options.showHudView   = NO;

    return options;
}
4,下载ffmpeg及openssl

时间较长,且可能超时

$ ./init-ios.sh
$ ./init-ios-openssl.sh
5,编译openssl及ffmpeg

clean ffmpeg

$ ./compile-ffmpeg.sh clean

编译openssl

$ ./compile-openssl.sh all

编译ffmpeg

$ ./compile-ffmpeg.sh all

若遇到问题
问题1:

AS  libavcodec/arm/aacpsdsp_neon.o
./libavutil/arm/asm.S:50:9: error: unknown directive
        .arch armv7-a
        ^
make: *** [libavcodec/arm/aacpsdsp_neon.o] Error 1
make: *** Waiting for unfinished jobs....

原因:Xcode 已经弱化了对 32 位的支持
解决办法:
~/ios/compile-ffmpeg.sh 删掉armv7

//24行改为:
FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"
//120行改为:
 if [ "$FF_TARGET" = "armv7s" -o "$FF_TARGET" = "arm64" ]; then
 //159行改为:
 echo "  compile-ffmpeg.sh arm64|i386|x86_64"

问题2:

ERROR: openssl not found

此问题报错处来源于~/extra/ffmpeg/configure

enabled openssl           && { use_pkg_config openssl openssl openssl/ssl.h OPENSSL_init_ssl ||
                               use_pkg_config openssl openssl openssl/ssl.h SSL_library_init ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
                              die "ERROR: openssl not found"; }

修改为:

enabled openssl           && { use_pkg_config openssl openssl openssl/ssl.h OPENSSL_init_ssl ||
   		                      check_lib openssl openssl/ssl.h OPENSSL_init_ssl -lssl -lcrypto ||
                              use_pkg_config openssl openssl openssl/ssl.h SSL_library_init ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                              check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
                              die "ERROR: openssl not found"; }

若依旧报错,检查mac是否安装了openssl

$ brew install openssl

注:编译失败,修改后重新编译需按照步骤clean ffmpeg、编译openssl、编译ffmpeg重新编译;之前编译ffmpeg失败后,修改后clean,再编译,一直报错ERROR: openssl not found,中间未进行openssl编译

6,打包IJKMediaFramework ~/ios/IJKMediaPlayer/IJKMediaPlayer.xcodeproj
选择IJKMediaFrameworkBuild Phases->Link Binary With Libraries添加libcrypto.a 和 libssl.a 文件,这两个文件在~/ios/build/universal/lib下Edit Scheme…->Run->Build Configuration 选择releaseXcode->Preferences->Locations

跳转至DerivedData处

编译真机下framework,Build Setting->Architectures,改为arm64,选择真机,command+B

Build成功后,在上步的DerivedData内则有刚编译成功的项目文件夹,~/Build/Products内则出现Release-iphoneos

编译模拟器下framework,Build Setting->Architectures,改为x86_64,选择模拟器,command+B

Build成功后,~/Build/Products内则出现Release-iphonesimulator

cd 至 Products下,合并真机与模拟器版本

lipo -create Release-iphoneos/IJKMediaFramework.framework/IJKMediaFramework Release-iphonesimulator/IJKMediaFramework.framework/IJKMediaFramework -output IJKMediaFramework

用合并后的新的IJKMediaFramework,替换掉~/Release-iphoneos/IJKMediaFramework.framework内的IJKMediaFramework
~/Release-iphoneos下的IJKMediaFramework.framework即为最终的framework

7,使用

工程内导入IJKMediaFramework.framework

添加依赖库

libc++.tbd
libz.tbd
libbz2.tbd
AudioToolbox.framework
UIKit.framework
CoreGraphics.framework
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
MediaPlayer.framework
MobileCoreServices.framework
OpenGLES.framework
QuartzCore.framework
VideoToolbox.framework

简单实现播放

#import 
#import 

@property(nonatomic, strong) idplayer;

NSString *videoUrl = @"xxxxxxx";

IJKFFOptions *options = [IJKFFOptions optionsByDefault]; //使用默认配置
self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:videoUrl] withOptions:options];
UIView *playerView = [self.player view];
playerView.frame = self.view.bounds;
playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:playerView];
[self.player setScalingMode:IJKMPMovieScalingModeAspectFit];
self.player.shouldAutoplay = YES;
[self.player prepareToPlay];

录制视频

@property (nonatomic, strong) NSString *savedVideoPath;

- (void)recordBtnCicked:(UIButton *)sender {

if (![self.player isRecording]) {
    
    [self.player startRecordWithFileName:[self getNowSavedVideoPath]];
    
}else{
    [self.player stopRecord];
    NSLog(@"保存的视频路径:%@",self.savedVideoPath);

    PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
    [photoLibrary performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
    fileURLWithPath:self.savedVideoPath]];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            NSLog(@"已将视频保存至相册");
        } else {
            NSLog(@"未能保存视频到相册");
        }
    }];
}
}
- (NSString *)getNowSavedVideoPath{
if (![self.player isRecording]) {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSError *error;
    NSString *defaultVideoPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"videoFile"];
    if (![[NSFileManager defaultManager]createDirectoryAtPath:defaultVideoPath withIntermediateDirectories:YES attributes:nil error:&error]) {
        NSLog(@"创建defaultVideoPath=============================%@",error);
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss:SSS"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    
    int i = arc4random() % 10000000 ;
    
    NSString *resultStr = [NSString stringWithFormat:@"%@_%d.mp4",timeSp,i];
    
    NSString *savedVideoPath = [defaultVideoPath stringByAppendingPathComponent:resultStr];
    
    self.savedVideoPath = savedVideoPath;
    }

    return self.savedVideoPath;
}
致谢,参考 ijkplayer开启rtsp,并且支持录制和截图功能ios ijkplayer改造添加录屏、RTSP、HTTPS支持iOS中集成ijkplayer视频直播框架iOS直播之ijkplayer的集成与简单使用(播放)ijkPlayer在Xcode10.3编译记录iOS-ijkPlayer集成问题ffmpeg 开启https, 提示“ERROR: openssl not found”ffmpeg链接第三方库OpenSSL问题“ERROR: openssl not found”openssl/ssl.h file not found mac 完美解决

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存