在一些視頻APP中,都可以看到一個(gè)將在線視頻轉(zhuǎn)成GIF圖的功能。下面就來說說思路以及實(shí)現(xiàn)。我們知道本地視頻可以生成GIF,那么將在線視頻截取成本地視頻不就可以了嗎?經(jīng)過比較,騰訊視頻App也是這么做的。話不多說,下面開始上代碼:
第一步:截取視頻
#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]; //創(chuàng)建AVMutableComposition對象來添加視頻音頻資源的AVMutableCompositionTrack AVMutableComposition* mixComposition = [AVMutableComposition composition]; //CMTimeRangeMake(start, duration),start起始時(shí)間,duration時(shí)長,都是CMTime類型 //CMTimeMake(int64_t value, int32_t timescale),返回CMTime,value視頻的一個(gè)總幀數(shù),timescale是指每秒視頻播放的幀數(shù),視頻播放速率,(value / timescale)才是視頻實(shí)際的秒數(shù)時(shí)長,timescale一般情況下不改變,截取視頻長度通過改變value的值 //CMTimeMakeWithSeconds(Float64 seconds, int32_t preferredTimeScale),返回CMTime,seconds截取時(shí)長(單位秒),preferredTimeScale每秒幀數(shù) //開始位置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]; // 避免數(shù)組越界 tracksWithMediaType 找不到對應(yīng)的文件時(shí)候返回空數(shù)組 //TimeRange截取的范圍長度 //ofTrack來源 //atTime插放在視頻的時(shí)間位置 [compositionVideoTrack insertTimeRange:videoTimeRange ofTrack:([videoAsset tracksWithMediaType:AVMediaTypeVideo].count>0) ? [videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject : nil atTime:kCMTimeZero error:nil]; //視頻聲音采集(也可不執(zhí)行這段代碼不采集視頻音軌,合并后的視頻文件將沒有視頻原來的聲音) 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用于合并文件,導(dǎo)出合并后文件,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; //輸出文件是否網(wǎng)絡(luò)優(yōu)化 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"); } } }); }]; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:iOS在線視頻生成GIF圖功能的方法-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article48/desihp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、響應(yīng)式網(wǎng)站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容