RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
iOS在线视频生成GIF图功能的方法-创新互联

在一些视频APP中,都可以看到一个将在线视频转成GIF图的功能。下面就来说说思路以及实现。我们知道本地视频可以生成GIF,那么将在线视频截取成本地视频不就可以了吗?经过比较,腾讯视频App也是这么做的。话不多说,下面开始上代码:

镜湖网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

第一步:截取视频

#pragma mark -截取视频
- (void)interceptVideoAndVideoUrl:(NSURL *)videoUrl withOutPath:(NSString *)outPath outputFileType:(NSString *)outputFileType range:(NSRange)videoRange intercept:(InterceptBlock)interceptBlock {
 
 _interceptBlock =interceptBlock;
 
 //不添加背景音乐
 NSURL *audioUrl =nil;
 //AVURLAsset此类主要用于获取媒体信息,包括视频、声音等
 AVURLAsset* audioAsset = [[AVURLAsset alloc] initWithURL:audioUrl options:nil];
 AVURLAsset* videoAsset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
 
 //创建AVMutableComposition对象来添加视频音频资源的AVMutableCompositionTrack
 AVMutableComposition* mixComposition = [AVMutableComposition composition];
 
 //CMTimeRangeMake(start, duration),start起始时间,duration时长,都是CMTime类型
 //CMTimeMake(int64_t value, int32_t timescale),返回CMTime,value视频的一个总帧数,timescale是指每秒视频播放的帧数,视频播放速率,(value / timescale)才是视频实际的秒数时长,timescale一般情况下不改变,截取视频长度通过改变value的值
 //CMTimeMakeWithSeconds(Float64 seconds, int32_t preferredTimeScale),返回CMTime,seconds截取时长(单位秒),preferredTimeScale每秒帧数
 
 //开始位置startTime
 CMTime startTime = CMTimeMakeWithSeconds(videoRange.location, videoAsset.duration.timescale);
 //截取长度videoDuration
 CMTime videoDuration = CMTimeMakeWithSeconds(videoRange.length, videoAsset.duration.timescale);
 
 CMTimeRange videoTimeRange = CMTimeRangeMake(startTime, videoDuration);
 
 //视频采集compositionVideoTrack
 AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
 
 // 避免数组越界 tracksWithMediaType 找不到对应的文件时候返回空数组
 //TimeRange截取的范围长度
 //ofTrack来源
 //atTime插放在视频的时间位置
 [compositionVideoTrack insertTimeRange:videoTimeRange ofTrack:([videoAsset tracksWithMediaType:AVMediaTypeVideo].count>0) ? [videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject : nil atTime:kCMTimeZero error:nil];
 
 
 //视频声音采集(也可不执行这段代码不采集视频音轨,合并后的视频文件将没有视频原来的声音)
 
 AVMutableCompositionTrack *compositionVoiceTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
 
 [compositionVoiceTrack insertTimeRange:videoTimeRange ofTrack:([videoAsset tracksWithMediaType:AVMediaTypeAudio].count>0)?[videoAsset tracksWithMediaType:AVMediaTypeAudio].firstObject:nil atTime:kCMTimeZero error:nil];
 
 //声音长度截取范围==视频长度
 CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero, videoDuration);
 
 //音频采集compositionCommentaryTrack
 AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
 
 [compositionAudioTrack insertTimeRange:audioTimeRange ofTrack:([audioAsset tracksWithMediaType:AVMediaTypeAudio].count > 0) ? [audioAsset tracksWithMediaType:AVMediaTypeAudio].firstObject : nil atTime:kCMTimeZero error:nil];
 
 //AVAssetExportSession用于合并文件,导出合并后文件,presetName文件的输出类型
 AVAssetExportSession *assetExportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
 
 
 //混合后的视频输出路径
 NSURL *outPutURL = [NSURL fileURLWithPath:outPath];
 
 if ([[NSFileManager defaultManager] fileExistsAtPath:outPath])
 {
  [[NSFileManager defaultManager] removeItemAtPath:outPath error:nil];
 }
 
 //输出视频格式
 assetExportSession.outputFileType = outputFileType;
 assetExportSession.outputURL = outPutURL;
 //输出文件是否网络优化
 assetExportSession.shouldOptimizeForNetworkUse = YES;
 [assetExportSession exportAsynchronouslyWithCompletionHandler:^{
  
  dispatch_async(dispatch_get_main_queue(), ^{
   
   switch (assetExportSession.status) {
    case AVAssetExportSessionStatusFailed:
     
     if (_interceptBlock) {
      
      _interceptBlock(assetExportSession.error,outPutURL);
     }
     
     
     break;
     
    case AVAssetExportSessionStatusCancelled:{
     
     logdebug(@"Export Status: Cancell");
     
     break;
    }
    case AVAssetExportSessionStatusCompleted: {
     
     if (_interceptBlock) {
      
      _interceptBlock(nil,outPutURL);
     }
     
     break;
    }
    case AVAssetExportSessionStatusUnknown: {
     
     logdebug(@"Export Status: Unknown");
    }
    case AVAssetExportSessionStatusExporting : {
     
     logdebug(@"Export Status: Exporting");
    }
    case AVAssetExportSessionStatusWaiting: {
     
     logdebug(@"Export Status: Wating");
    }
     
     
   }
   
   
  });
  
  
 }];
}

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:iOS在线视频生成GIF图功能的方法-创新互联
网页URL:http://ncwzjz.com/article/desihp.html