移动和修复在手机 iOS 上录制的视频的 moov atom

Krz*_*ski 1 video-streaming ios

问题是如何找到并移动记录在 iOS 设备上的 .mov 文件的 moov 原子,以便您可以通过 http 流式传输它。有一种方法可以做到这一点,但需要将其导出到文件,理论上这可以让您复制整个文件,然后您就可以流式传输它。

有没有其他方法可以做到这一点?

pra*_*iya 5

  • 使用iOS AV Foundation框架和几行Objective-C(您也可以从MOV转换为MP4,因为Android无法读取MOV):

    因此,使用此代码无需缓冲即可从 Live URL 播放流畅的视频,但在将视频上传到您的服务器之前,请使用此代码并转换您的视频,然后再上传。所以视频是在没有任何负载的情况下播放像 snapchat 这样的视频。

    不要忘记将以下框架添加到您的项目中。

#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetExportSession.h>
#import <AVFoundation/AVMediaFormat.h>
Run Code Online (Sandbox Code Playgroud)
+ (void) convertVideoToMP4AndFixMooV: (NSString*)filename toPath:(NSString*)outputPath {

    NSURL *url = [NSURL fileURLWithPath:filename];
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:avAsset
                                           presetName:AVAssetExportPresetPassthrough];

    exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
    exportSession.outputFileType = AVFileTypeAppleM4V;

    // This should move the moov atom before the mdat atom,
    // hence allow playback before the entire file is downloaded
    exportSession.shouldOptimizeForNetworkUse = YES;

    [exportSession exportAsynchronouslyWithCompletionHandler:
     ^{

         if (AVAssetExportSessionStatusCompleted == exportSession.status) {}
         else if (AVAssetExportSessionStatusFailed == exportSession.status) {
             NSLog(@"AVAssetExportSessionStatusFailed");
         }
         else
         {
             NSLog(@"Export Session Status: %d", exportSession.status);
         }
     }];
}
Run Code Online (Sandbox Code Playgroud)

  • 这可行,但如果您有 4 分钟长的视频怎么办?转换然后进行流式传输可能需要 30 秒以上的时间。 (2认同)