菜鸟教程小白 发表于 2022-12-12 09:37:19

ios - 拼接两个视频


                                            <p><p>我需要创建一个由两个拼接视频组成的视频,如下图:</p>

<p> <img src="/image/RmaWF.png" alt="Two videos playing simultaneously - iOS simulator"/> </p>

<p>实际上,我正在使用两个 AVPlayer 实例同时播放两个视频,但我需要使用这两个视频创建最终视频:Final video = </p>

<p>您对如何做到这一点有任何想法吗?这是我用来播放这两个视频的代码:</p>

<pre><code>- (void)viewDidLoad
{
    ;
    NSURL *url = [ URLForResource:@&#34;video_1&#34; withExtension:@&#34;mp4&#34;];

    self.mPlayer = ;
    self.mPlayer2 = ;

    ;
    ;
}

- (void)observeValueForKeyPath:(NSString*)path ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
    if (]) {
      if () {
            switch(item.status) {
                case AVPlayerItemStatusFailed:
                  NSLog(@&#34;player item status failed&#34;);
                  break;
                case AVPlayerItemStatusReadyToPlay:
                  NSLog(@&#34;player item status is ready to play&#34;);
                  ;
                  ;
                  ;
                  ;
                  break;
                case AVPlayerItemStatusUnknown:
                  NSLog(@&#34;player item status is unknown&#34;);
                  break;
            }
      }
    }
}
</code></pre>

<p>来源:<a href="https://abdulazeem.wordpress.com/2012/04/02/" rel="noreferrer noopener nofollow">https://abdulazeem.wordpress.com/2012/04/02/</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我找到了解决方案。我来了:</p>

<pre><code>- (void)stitchAudio:(NSURL *)file1 audio2:(NSURL *)file2 {
    AVAsset *video1Asset = ;
    AVAsset *video2Asset = ;

    AVMutableComposition* mixComposition = ;

    AVMutableCompositionTrack *firstTrack = ;
    [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1Asset.duration)
                        ofTrack:[ objectAtIndex:0]
                         atTime:kCMTimeZero error:nil];

    AVMutableCompositionTrack *secondTrack = ;

    [secondTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2Asset.duration)
                         ofTrack:[ objectAtIndex:0]
                        atTime:kCMTimeZero error:nil];

    //See how we are creating AVMutableVideoCompositionInstruction object.This object will contain the array of our AVMutableVideoCompositionLayerInstruction objects.You set the duration of the layer.You should add the lenght equal to the lingth of the longer asset in terms of duration.
    AVMutableVideoCompositionInstruction * MainInstruction = ;
    MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, video1Asset.duration);

    //We will be creating 2 AVMutableVideoCompositionLayerInstruction objects.Each for our 2 AVMutableCompositionTrack.here we are creating AVMutableVideoCompositionLayerInstruction for out first track.see how we make use of Affinetransform to move and scale our First Track.so it is displayed at the bottom of the screen in smaller size.(First track in the one that remains on top).
    AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = ;
    CGAffineTransform Scale = CGAffineTransformMakeScale(0.68f,0.68f);
    CGAffineTransform Move = CGAffineTransformMakeTranslation(0,0);
    ;

    //Here we are creating AVMutableVideoCompositionLayerInstruction for out second track.see how we make use of Affinetransform to move and scale our second Track.
    AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction = ;
    CGAffineTransform SecondScale = CGAffineTransformMakeScale(0.68f,0.68f);
    CGAffineTransform SecondMove = CGAffineTransformMakeTranslation(318,0);
    ;

    //Now we add our 2 created AVMutableVideoCompositionLayerInstruction objects to our AVMutableVideoCompositionInstruction in form of an array.
    MainInstruction.layerInstructions = ;

    //Now we create AVMutableVideoComposition object.We can add mutiple AVMutableVideoCompositionInstruction to this object.We have only one AVMutableVideoCompositionInstruction object in our example.You can use multiple AVMutableVideoCompositionInstruction objects to add multiple layers of effects such as fade and transition but make sure that time ranges of the AVMutableVideoCompositionInstruction objects dont overlap.
    AVMutableVideoComposition *MainCompositionInst = ;
    MainCompositionInst.instructions = ;
    MainCompositionInst.frameDuration = CMTimeMake(1, 30);
    MainCompositionInst.renderSize = CGSizeMake(640, 480);

    //Finally just add the newly created AVMutableComposition with multiple tracks to an AVPlayerItem and play it using AVPlayer.
    AVPlayerItem * newPlayerItem = ;
    newPlayerItem.videoComposition = MainCompositionInst;
    self.mPlayer = ;
    ;
    ;
   // ;

    // Create the export session with the composition and set the preset to the highest quality.
    AVAssetExportSession *exporter = [ initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    // Set the desired output URL for the file created by the export process.
    exporter.outputURL = ;
    exporter.videoComposition = MainCompositionInst;
    // Set the output file type to be a QuickTime movie.
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.shouldOptimizeForNetworkUse = YES;

    // Asynchronously export the composition to a video file and save this file to the camera roll once export completes.
    [exporter exportAsynchronouslyWithCompletionHandler:^{
      dispatch_async(dispatch_get_main_queue(), ^{
            if (exporter.status == AVAssetExportSessionStatusCompleted) {
                ALAssetsLibrary *assetsLibrary = [ init];

                self.mPlayer = ;
                //self.mPlayer2 = ;

                //;

                if () {
                  ;
                }
            }
      });
    }];
}
</code></pre>

<p>来源:<a href="https://abdulazeem.wordpress.com/2012/04/02/" rel="noreferrer noopener nofollow">https://abdulazeem.wordpress.com/2012/04/02/</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 拼接两个视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23428582/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23428582/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 拼接两个视频