iOS AVDemo(11):視頻轉(zhuǎn)封裝,從 MP4 到 MP4丨音視頻工程示例

vx 搜索『gjzkeyframe』 關(guān)注『關(guān)鍵幀Keyframe』來及時獲得最新的音視頻技術(shù)文章。

莫奈《孔塔里尼法桑宮》

這個公眾號會路線圖****式的遍歷分享音視頻技術(shù)音視頻基礎(chǔ)(完成)音視頻工具(完成)音視頻工程示例(進行中) → 音視頻工業(yè)實戰(zhàn)(準備)。

iOS/Android 客戶端開發(fā)同學如果想要開始學習音視頻開發(fā),最絲滑的方式是對音視頻基礎(chǔ)概念知識有一定了解后,再借助 iOS/Android 平臺的音視頻能力上手去實踐音視頻的采集 → 編碼 → 封裝 → 解封裝 → 解碼 → 渲染過程,并借助音視頻工具來分析和理解對應(yīng)的音視頻數(shù)據(jù)。

音視頻工程示例這個欄目,我們將通過拆解采集 → 編碼 → 封裝 → 解封裝 → 解碼 → 渲染流程并實現(xiàn) Demo 來向大家介紹如何在 iOS/Android 平臺上手音視頻開發(fā)。

這里是第十一篇:iOS 視頻轉(zhuǎn)封裝 Demo。這個 Demo 里包含以下內(nèi)容:
1)實現(xiàn)一個音視頻解封裝模塊;
2)實現(xiàn)一個音視頻封裝模塊;
3)實現(xiàn)對 MP4 文件中音視頻的解封裝邏輯,將解封裝后的音視頻編碼數(shù)據(jù)重新封裝存儲為一個新的 MP4 文件;
4)詳盡的代碼注釋,幫你理解代碼邏輯和原理。
在本文中,我們將詳解一下 Demo 的具體實現(xiàn)和源碼。讀完本文內(nèi)容相信就能幫你掌握相關(guān)知識。

不過,如果你的需求是:1)直接獲得全部工程源碼;2)想進一步咨詢音視頻技術(shù)問題;3)咨詢音視頻職業(yè)發(fā)展問題??梢愿鶕?jù)自己的需要考慮是否加入『關(guān)鍵幀的音視頻開發(fā)圈』,這是一個收費的社群服務(wù),目前還有少量優(yōu)惠券可用。vx 搜索『gjzkeyframe』 關(guān)注『關(guān)鍵幀Keyframe』咨詢,或知識星球搜『關(guān)鍵幀的音視頻開發(fā)圈』即可加入。

1、音視頻解封裝模塊

視頻編碼模塊即 KFMP4Demuxer,復(fù)用了《iOS 音頻解封裝 Demo》中介紹的 demuxer,這里就不再重復(fù)介紹了,其接口如下:

KFMP4Demuxer.h

#import <Foundation/Foundation.h>
#import <CoreMedia/CoreMedia.h>
#import "KFMuxerConfig.h"

NS_ASSUME_NONNULL_BEGIN

@interface KFMP4Muxer : NSObject
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithConfig:(KFMuxerConfig *)config;

@property (nonatomic, strong, readonly) KFMuxerConfig *config;
@property (nonatomic, copy) void (^errorCallBack)(NSError *error); // 封裝錯誤回調(diào)。

- (void)startWriting; // 開始封裝寫入數(shù)據(jù)。
- (void)cancelWriting; // 取消封裝寫入數(shù)據(jù)。
- (void)appendSampleBuffer:(CMSampleBufferRef)sampleBuffer; // 添加封裝數(shù)據(jù)。
- (void)stopWriting:(void (^)(BOOL success, NSError *error))completeHandler; // 停止封裝寫入數(shù)據(jù)。
@end

NS_ASSUME_NONNULL_END

2、音視頻封裝模塊

視頻編碼模塊即 KFMP4Muxer,復(fù)用了《iOS 音頻封裝 Demo》中介紹的 muxer,這里就不再重復(fù)介紹了,其接口如下:

KFMP4Muxer.h

#import <Foundation/Foundation.h>
#import <CoreMedia/CoreMedia.h>
#import "KFMuxerConfig.h"

NS_ASSUME_NONNULL_BEGIN

@interface KFMP4Muxer : NSObject
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithConfig:(KFMuxerConfig *)config;

@property (nonatomic, strong, readonly) KFMuxerConfig *config;
@property (nonatomic, copy) void (^errorCallBack)(NSError *error); // 封裝錯誤回調(diào)。

- (void)startWriting; // 開始封裝寫入數(shù)據(jù)。
- (void)cancelWriting; // 取消封裝寫入數(shù)據(jù)。
- (void)appendSampleBuffer:(CMSampleBufferRef)sampleBuffer; // 添加封裝數(shù)據(jù)。
- (void)stopWriting:(void (^)(BOOL success, NSError *error))completeHandler; // 停止封裝寫入數(shù)據(jù)。
@end

NS_ASSUME_NONNULL_END

3、音視頻重封裝邏輯

我們還是在一個 ViewController 中來實現(xiàn)對 MP4 文件中音視頻的解封裝邏輯,然后將解封裝后的音視頻編碼數(shù)據(jù)重新封裝存儲為一個新的 MP4 文件。
KFRemuxerViewController.m

#import "KFRemuxerViewController.h"
#import "KFMP4Demuxer.h"
#import "KFMP4Muxer.h"

@interface KFRemuxerViewController ()
@property (nonatomic, strong) KFDemuxerConfig *demuxerConfig;
@property (nonatomic, strong) KFMP4Demuxer *demuxer;
@property (nonatomic, strong) KFMuxerConfig *muxerConfig;
@property (nonatomic, strong) KFMP4Muxer *muxer;
@end

@implementation KFRemuxerViewController
#pragma mark - Property
- (KFDemuxerConfig *)demuxerConfig {
    if (!_demuxerConfig) {
        _demuxerConfig = [[KFDemuxerConfig alloc] init];
        _demuxerConfig.demuxerType = KFMediaAV;
        NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"input" ofType:@"mp4"];
        _demuxerConfig.asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:videoPath]];
    }
    
    return _demuxerConfig;
}

- (KFMP4Demuxer *)demuxer {
    if (!_demuxer) {
        _demuxer = [[KFMP4Demuxer alloc] initWithConfig:self.demuxerConfig];
        _demuxer.errorCallBack = ^(NSError *error) {
            NSLog(@"KFMP4Demuxer error:%zi %@", error.code, error.localizedDescription);
        };
    }
    
    return _demuxer;
}

- (KFMuxerConfig *)muxerConfig {
    if (!_muxerConfig) {
        _muxerConfig = [[KFMuxerConfig alloc] init];
        NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"output.mp4"];
        NSLog(@"MP4 file path: %@", videoPath);
        [[NSFileManager defaultManager] removeItemAtPath:videoPath error:nil];
        _muxerConfig.outputURL = [NSURL fileURLWithPath:videoPath];
        _muxerConfig.muxerType = KFMediaAV;
    }
    
    return _muxerConfig;
}

- (KFMP4Muxer *)muxer {
    if (!_muxer) {
        _muxer = [[KFMP4Muxer alloc] initWithConfig:self.muxerConfig];
    }
    
    return _muxer;
}

#pragma mark - Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"AV Remuxer";
    UIBarButtonItem *startBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(start)];
    self.navigationItem.rightBarButtonItems = @[startBarButton];
}

#pragma mark - Action
- (void)start {
    __weak typeof(self) weakSelf = self;
    NSLog(@"KFMP4Demuxer start");
    [self.demuxer startReading:^(BOOL success, NSError * _Nonnull error) {
        if (success) {
            // Demuxer 啟動成功后,就可以從它里面獲取解封裝后的數(shù)據(jù)了。
            [weakSelf fetchAndRemuxData];
        }else{
            NSLog(@"KFDemuxer error: %zi %@", error.code, error.localizedDescription);
        }
    }];
}

#pragma mark - Utility
- (void)fetchAndRemuxData {
    // 異步地從 Demuxer 獲取解封裝后的 H.264/H.265 編碼數(shù)據(jù),再重新封裝。
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self.muxer startWriting];
        while (self.demuxer.hasVideoSampleBuffer || self.demuxer.hasAudioSampleBuffer) {
            CMSampleBufferRef videoBuffer = [self.demuxer copyNextVideoSampleBuffer];
            if (videoBuffer) {
                [self.muxer appendSampleBuffer:videoBuffer];
                CFRelease(videoBuffer);
            }
            
            CMSampleBufferRef audioBuffer = [self.demuxer copyNextAudioSampleBuffer];
            if (audioBuffer) {
                [self.muxer appendSampleBuffer:audioBuffer];
                CFRelease(audioBuffer);
            }
        }
        if (self.demuxer.demuxerStatus == KFMP4DemuxerStatusCompleted) {
            NSLog(@"KFMP4Demuxer complete");
            [self.muxer stopWriting:^(BOOL success, NSError * _Nonnull error) {
                NSLog(@"KFMP4Muxer complete:%d", success);
            }];
        }
    });
}

@end

上面是KFRemuxerViewController 的實現(xiàn),其中主要包含這幾個部分:

  • 1)設(shè)置好待解封裝的資源。
    -demuxerConfig 中實現(xiàn),我們這里是一個 MP4 文件。
  • 2)啟動解封裝器。
    -start 中實現(xiàn)。
  • 3)在解封裝器啟動成功后,啟動封裝器。
    -fetchAndRemuxData 中啟動。
  • 4)讀取解封裝后的音視頻編碼數(shù)據(jù)并送給封裝器進行重新封裝。
    -fetchAndRemuxData 中實現(xiàn)。

4、用工具播放 MP4 文件

完成 Demo 后,可以將 App Document 文件夾下面的 output.mp4 文件拷貝到電腦上,使用 ffplay 播放來驗證一下效果是否符合預(yù)期:

關(guān)于播放 MP4 文件的工具,可以參考《FFmpeg 工具》第 2 節(jié) ffplay 命令行工具《可視化音視頻分析工具》第 3.5 節(jié) VLC 播放器

$ ffplay -I output.mp4

我們還可以用《可視化音視頻分析工具》第 3.1 節(jié) MP4Box.js 等工具來查看它的格式。

- 完 -

《iOS AVDemo(10):視頻解封裝》

《iOS AVDemo(9):視頻封裝》

《iOS AVDemo(8):視頻編碼》

《iOS AVDemo(7):視頻采集》

《iOS 音頻處理框架及重點 API 合集》

《iOS AVDemo(6):音頻渲染》

《iOS AVDemo(5):音頻解碼》

《iOS AVDemo(4):音頻解封裝》

《iOS AVDemo(3):音頻封裝》

《iOS AVDemo(2):音頻編碼》

《iOS AVDemo(1):音頻采集》

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容