用ffmpeg來(lái)處理音視頻格式問(wèn)題以及錄屏的裸數(shù)據(jù)轉(zhuǎn)mp4

文章是以實(shí)現(xiàn)iOS12+的錄屏功能(RPSystemBroadcastPickerView)來(lái)進(jìn)行闡述遇到的問(wèn)題,后續(xù)也會(huì)繼續(xù)補(bǔ)充相關(guān)技術(shù)點(diǎn)。方式是利用添加擴(kuò)展(RPBroadcastSampleHandler)的方式來(lái)實(shí)現(xiàn)錄屏功能,進(jìn)而闡述如何使用ffmpeg進(jìn)行視頻格式的轉(zhuǎn)碼。本文難點(diǎn)有兩點(diǎn):1,擴(kuò)展中獲取到的原始數(shù)據(jù)CMSampleBufferRef如何傳遞到宿主App;2,宿主App接收到擴(kuò)展(SampleHandler)傳遞的視頻流如何對(duì)其進(jìn)行硬編碼,最后實(shí)現(xiàn)格式轉(zhuǎn)碼。

添加擴(kuò)展,調(diào)取系統(tǒng)錄屏的方法

添加擴(kuò)展

自動(dòng)生成的擴(kuò)展文件
  • 錄屏的方法
// 開(kāi)始錄屏
- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {

//    // 監(jiān)聽(tīng) 綁定socket
    [[HYLocalServerBufferSocketManager defaultManager] setupSocket];

    // 開(kāi)始錄屏?xí)r發(fā)出通知
    [self sendNotificationForMessageWithIdentifier:@"broadcastStartedWithSetupInfo" userInfo:nil];
}

// 暫停錄屏
- (void)broadcastPaused {

    [[HYLocalServerBufferSocketManager defaultManager] disConnectSocket];
    [self sendNotificationForMessageWithIdentifier:@"broadcastPaused" userInfo:nil];
}

// 恢復(fù)錄屏
- (void)broadcastResumed {

    [[HYLocalServerBufferSocketManager defaultManager] disConnectSocket];
    [self sendNotificationForMessageWithIdentifier:@"broadcastResumed" userInfo:nil];
}

// 結(jié)束錄屏
- (void)broadcastFinished {

    [[HYLocalServerBufferSocketManager defaultManager] disConnectSocket];
    [self sendNotificationForMessageWithIdentifier:@"broadcastFinished" userInfo:nil];
}

// 這個(gè)方法是實(shí)時(shí)獲取錄屏直播,所以不要在這個(gè)方法中寫(xiě)發(fā)通知(同步),會(huì)造成線(xiàn)程阻塞的問(wèn)題
- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
    switch (sampleBufferType) {
        case RPSampleBufferTypeVideo:
            @autoreleasepool { 
                [[HYLocalServerBufferSocketManager defaultManager] sendVideoBufferToHostApp:sampleBuffer];
            }
            break;
        case RPSampleBufferTypeAudioApp:
            break;
        case RPSampleBufferTypeAudioMic:
            // Handle audio sample buffer for mic audio
            break;
            
        default:
            break;
    }
}
  • 錄屏的喚起方法
    if (@available(iOS 12.0, *)) {

        RPSystemBroadcastPickerView *broadcastPickerView = [[RPSystemBroadcastPickerView alloc] initWithFrame: [UIScreen mainScreen].bounds];
        broadcastPickerView.showsMicrophoneButton = YES;
        //你的app對(duì)用upload extension的 bundle id, 必須要填寫(xiě)對(duì)
        if (@available(iOS 12.0, *)) {
            broadcastPickerView.preferredExtension = @"com.hyTechnology.ffmpegDemo.XIBOBroadcastUploadExtension";
        }
        // 間接移除系統(tǒng)錄屏自帶的錄屏按鈕
        for (UIView *view in broadcastPickerView.subviews) {
            if ([view isKindOfClass:[UIButton class]])  {
                [(UIButton*)view sendActionsForControlEvents:UIControlEventTouchUpInside];
            }
        }
    }

上面就正式完成了iOS12+喚起系統(tǒng)自帶錄屏功能的擴(kuò)展及調(diào)用方式

數(shù)據(jù)傳遞方式

  • local socket
  • 通知(CFNotificationCenterRef)// 進(jìn)程間的通知方式,由于擴(kuò)展和宿主App分別是兩個(gè)獨(dú)立的運(yùn)行模塊,所以是兩個(gè)進(jìn)程。
  • App Group

注意點(diǎn):利用CFNotificationCenterRef的方式傳遞數(shù)據(jù)時(shí),無(wú)法傳遞實(shí)時(shí)采集的原始數(shù)據(jù), 所以我這里利用通知來(lái)傳遞錄屏的狀態(tài), 有可能是自己方式不對(duì),后續(xù)繼續(xù)深入去了解... 。

方式一:通知方式傳遞
void NotificationCallback(CFNotificationCenterRef center,
                                   void * observer,
                                   CFStringRef name,
                                   void const * object,
                                   CFDictionaryRef userInfo) {
    NSString *identifier = (__bridge NSString *)name;
    NSObject *sender = (__bridge NSObject *)observer;
    // 使用通知打印的userInfo為空, 這里我傳的是采集的原始幀數(shù)據(jù)
//    NSDictionary *info = (__bridge NSDictionary *)userInfo;
//    NSDictionary *infoss= CFBridgingRelease(userInfo);
    NSDictionary *notiUserInfo = @{@"identifier":identifier};
    [[NSNotificationCenter defaultCenter] postNotificationName:ScreenHoleNotificationName
                                                        object:sender
                                                      userInfo:notiUserInfo];
}
方式二:local socket

這種方式是參照阿里云的智能雙錄質(zhì)檢的幫助中心 iOS屏幕共享使用說(shuō)明的文檔。socket是一對(duì)套接字,服務(wù)端的套接字運(yùn)行在擴(kuò)展中,客戶(hù)端的套接字運(yùn)行在宿主App中。服務(wù)端的socket會(huì)先將CMSampleBufferRef轉(zhuǎn)為 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);最后利用NTESI420FrameCVPixelBufferRef轉(zhuǎn)為NSData發(fā)給對(duì)應(yīng)得Host App的socket。

  • 1,創(chuàng)建服務(wù)端和客戶(hù)端的socket。
  • 2,服務(wù)端的socket將采集到的CMSampleBufferRef原始幀視頻轉(zhuǎn)為NSData格式發(fā)給客戶(hù)端的socket。
  • 3,客戶(hù)端的socket接收到數(shù)據(jù)后再對(duì)數(shù)據(jù)進(jìn)行解碼得到原始幀數(shù)據(jù)CMSampleBufferRef

利用ffmpeg進(jìn)行裸幀數(shù)據(jù)的格式編碼

  • 安裝ffmpeg
1, 安裝過(guò)程Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2, 安裝 gas-preprocessor
sudo git clone https://github.com/bigsen/gas-preprocessor.git /usr/local/bin/gas
sudo cp /usr/local/bin/gas/gas-preprocessor.pl /usr/local/bin/gas-preprocessor.pl
sudo chmod 777 /usr/local/bin/gas-preprocessor.pl
sudo rm -rf /usr/local/bin/gas/

3, 安裝 yams
brew info yasm

4, 下載ffmpeg
brew install ffmpeg

5,進(jìn)入文件執(zhí)行下列命令
./build-ffmpeg.sh

注意: 再install ffmpeg的時(shí)候可能會(huì)失敗(eg:下圖1),按照提示install 即可,成功之后重新install ffmpeg

install ffmpeg 失敗 1
安裝成功后自動(dòng)生成的文件

iOS集成ffmpeg

當(dāng)我們下載ffmpeg腳本后進(jìn)入文件夾使用./build-ffmpeg.sh就會(huì)自動(dòng)生成FFmpeg-iOS等文件(如下圖)

編譯過(guò)后生成的ffmpeg文件

  • 第一步:將FFmpeg-iOS文件夾拖進(jìn)工程
  • 第二步:配置頭文件的搜索路徑在工程文件->Bulid Setting->Search Paths->Header Search Paths添加"$(SRCROOT)/ffmpegDemo/FFmpeg-iOS/include"
  • 第三部:配置靜態(tài)庫(kù)等文件


    配置靜態(tài)庫(kù)等文件
  • 第四部: 添加fftools文件夾,tools文件中不是所有的都需要,按自己需求添加


    fftools
  • 第五步:#import "ffmpeg.h"并在工程中寫(xiě)入av_register_all();后?commond+B編譯一下,這里會(huì)某些文件找不到的報(bào)錯(cuò),可直接到編譯后的腳本文件夾中
例如:config.h文件找不到,可以到下圖的文件夾中添加即可,其他同理
#include "libavutil/thread.h"
#include "libavutil/reverse.h"
#include "libavutil/libm.h"
#include "libpostproc/postprocess.h"
#include "libpostproc/version.h"
#include "libavformat/os_support.h"
#include "libavcodec/mathops.h"
#include "libavresample/avresample.h"
#include "compat/va_copy.h" 
#include "libavutil/internal.h"
編譯文件中的config文件

如果項(xiàng)目不要用到相關(guān)的文件,可以直接注釋錯(cuò)誤
eg: // #include "libavutil/internal.h"

ffmpeg的格式轉(zhuǎn)換

    NSString *fromFile = [[NSBundle mainBundle]pathForResource:@"videp.mp4" ofType:nil];
    
    NSString *toFile = @"/Users/Alex/output/source/video.gif";
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:toFile]) {
        [fileManager removeItemAtPath:toFile error:nil];
    }
    char *a[] = {
        "ffmpeg", "-i", (char *)[fromFile UTF8String], (char *)[toFile UTF8String]
    };

    int result = ffmpeg_main(sizeof(a)/sizeof(*a), a);
    NSLog(@"這是結(jié)果 %d",result);
或

ffmpeg –i test.mp4 –vcodec h264 –s 352*278 –an –f m4v video.264              //轉(zhuǎn)碼為碼流原始文件
ffmpeg –i test.mp4 –vcodec h264 –bf 0 –g 25 –s 352*278 –an –f m4v video.264  //轉(zhuǎn)碼為碼流原始文件
ffmpeg –i test.avi -vcodec mpeg4 –vtag xvid –qsame test_xvid.avi            //轉(zhuǎn)碼為封裝文件
//-bf B幀數(shù)目控制,-g 關(guān)鍵幀間隔控制,-s 分辨率控制

思路解析
我們?cè)谔砑訑U(kuò)展后拿到的一般是CMSampleBufferRef格式的原始數(shù)據(jù),而這種格式的數(shù)據(jù)不能進(jìn)行進(jìn)程間的直接傳遞,以及播放。所以需要進(jìn)行處理和轉(zhuǎn)碼。
1,將CMSampleBufferRef格式轉(zhuǎn)為.264(NSData)的格式
2,可以使用ffmpeg的轉(zhuǎn)碼將.264(NSData)轉(zhuǎn)為MP4,需要注意的是設(shè)置的size,不對(duì)的話(huà),可能會(huì)造成視頻的拉伸或擠壓。

將CMSampleBufferRef格式的幀數(shù)據(jù)轉(zhuǎn)成mp4,并保存到沙盒中

思路:

  • 1,利用socket、通知、AppGroup的方式將擴(kuò)展中中實(shí)時(shí)采集的幀數(shù)據(jù)CMSampleBufferRef傳遞到宿主App中


    Snip20210912_9.png
  • 2,利用AVCaptureSession、AVAssetWriter、AVAssetWriterInput將CMSampleBufferRef轉(zhuǎn)為mp4, 并[圖片上傳中...(Snip20210912_7.png-aa8236-1631443458420-0)]
    利用NSFileManger保存到指定沙盒

- (void)startScreenRecording {
    
    self.captureSession = [[AVCaptureSession alloc]init];
    self.screenRecorder = [RPScreenRecorder sharedRecorder];
    if (self.screenRecorder.isRecording) {
        return;
    }
    NSError *error = nil;
    NSArray *pathDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *outputURL = pathDocuments[0];
    self.videoOutPath = [[outputURL stringByAppendingPathComponent:@"demo是嗯嗯嗯是是"] stringByAppendingPathExtension:@"mp4"];
    NSLog(@"self.videoOutPath=%@",self.videoOutPath);
    
    self.assetWriter = [AVAssetWriter assetWriterWithURL:[NSURL fileURLWithPath:self.videoOutPath] fileType:AVFileTypeMPEG4 error:&error];
    
    NSDictionary *compressionProperties =
        @{AVVideoProfileLevelKey         : AVVideoProfileLevelH264HighAutoLevel,
          AVVideoH264EntropyModeKey      : AVVideoH264EntropyModeCABAC,
          AVVideoAverageBitRateKey       : @(1920 * 1080 * 11.4),
          AVVideoMaxKeyFrameIntervalKey  : @60,
          AVVideoAllowFrameReorderingKey : @NO};
    
    NSNumber* width= [NSNumber numberWithFloat:[[UIScreen mainScreen] bounds].size.width];
    NSNumber* height = [NSNumber numberWithFloat:[[UIScreen mainScreen] bounds].size.height];
    
    NSDictionary *videoSettings =
        @{
          AVVideoCompressionPropertiesKey : compressionProperties,
          AVVideoCodecKey                 : AVVideoCodecTypeH264,
          AVVideoWidthKey                 : width,
          AVVideoHeightKey                : height
          };
    
    self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
    self.pixelBufferAdaptor =
    [[AVAssetWriterInputPixelBufferAdaptor alloc]initWithAssetWriterInput:self.assetWriterInput
                                              sourcePixelBufferAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA],kCVPixelBufferPixelFormatTypeKey,nil]];
    [self.assetWriter addInput:self.assetWriterInput];
    [self.assetWriterInput setMediaTimeScale:60];
    [self.assetWriter setMovieTimeScale:60];
    [self.assetWriterInput setExpectsMediaDataInRealTime:YES];
    
    //寫(xiě)入視頻
    [self.assetWriter startWriting];
    [self.assetWriter startSessionAtSourceTime:kCMTimeZero];
    
    [self.captureSession startRunning];
    
}
  • 3,開(kāi)始錄屏?xí)r初始化assetWriter,結(jié)束錄屏?xí)r進(jìn)行寫(xiě)入完成操作


    初始化assetWriter和完成

總結(jié)

整個(gè)過(guò)程中最簡(jiǎn)單的方式還是使用AppGroup的方式進(jìn)行進(jìn)程間的傳值操作,通知方式CFNotificationCenterRef不能傳遞錄屏的數(shù)據(jù),所以在項(xiàng)目中使用通知是監(jiān)聽(tīng)錄屏的狀態(tài)statussocket的方式需要用到GCDAsyncSocket,在套接字之間傳的是NTESI420Frame轉(zhuǎn)化的NSData,最后再轉(zhuǎn)回為CMSampleBufferRef。類(lèi)似剪映等產(chǎn)品的錄屏應(yīng)該都是使用AVAssetWriter、AVAssetWriterInput、AVAssetWriterInputPixelBufferAdaptor、AVCaptureSession來(lái)處理原始的幀數(shù)據(jù)后保存到沙盒中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評(píng)論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,582評(píng)論 3 418
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 176,540評(píng)論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,028評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,801評(píng)論 6 410
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,223評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,442評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,976評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,800評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,996評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評(píng)論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,233評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,662評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,926評(píng)論 1 286
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,702評(píng)論 3 392
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,991評(píng)論 2 374

推薦閱讀更多精彩內(nèi)容