После обрезки iOS-видео в портретном режиме и сохранения обрезанное видео становится альбомным - PullRequest
0 голосов
/ 18 июня 2019

После обрезки на Портрет, мое новое видео на Пейзаж

public static Task<bool> Crop(NSUrl source, NSUrl destination, double startSeconds, double durationSeconds)
    {
        TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();

        AVUrlAsset videoAsset = AVAsset.FromUrl(source) as AVUrlAsset;

        CGAffineTransform rotateTranslate;
        CGSize renderSize = videoAsset.NaturalSize; //new CGSize(videoAsset.NaturalSize.Height, videoAsset.NaturalSize.Width);

        AVAssetTrack sourceVideoTrack = videoAsset.TracksWithMediaType(AVMediaType.Video).FirstOrDefault();
        // AVAssetTrack sourceAudioTrack = videoAsset.TracksWithMediaType(AVMediaType.Audio).FirstOrDefault();

        AVMutableComposition composition = new AVMutableComposition();

        AVMutableCompositionTrack compositionVideoTrack = composition.AddMutableTrack(AVMediaType.Video, 0);

        compositionVideoTrack.InsertTimeRange(
            new CMTimeRange { Start = CMTime.FromSeconds(startSeconds, 1000), Duration = CMTime.FromSeconds(durationSeconds, 1000) },
            sourceVideoTrack,
            CMTime.Zero, out NSError error
        );

        //AVMutableCompositionTrack compositionAudioTrack = composition.AddMutableTrack(AVMediaType.Video, 0);

        AVMutableVideoCompositionInstruction instruction = new AVMutableVideoCompositionInstruction();
        AVMutableVideoCompositionLayerInstruction layerInstruction = AVMutableVideoCompositionLayerInstruction.FromAssetTrack(compositionVideoTrack);

        rotateTranslate = CGAffineTransform.MakeRotation(0);
        rotateTranslate = CGAffineTransform.Translate(rotateTranslate, 0, 0);

        layerInstruction.SetTransform(rotateTranslate, CMTime.Zero);

        AVMutableVideoComposition videoComposition = new AVMutableVideoComposition();
        videoComposition.FrameDuration = new CMTime(1, 30);
        videoComposition.RenderScale = 1.0f;
        videoComposition.RenderSize = renderSize;
        instruction.LayerInstructions = new AVVideoCompositionLayerInstruction[] { layerInstruction };
        instruction.TimeRange = new CMTimeRange { Start = CMTime.Zero, Duration = CMTime.FromSeconds(durationSeconds, 1000) };
        videoComposition.Instructions = new AVVideoCompositionInstruction[] { instruction };

        AVAssetExportSession assetExport = AVAssetExportSession.FromAsset(composition, "AVAssetExportPresetMediumQuality");

        assetExport.OutputFileType = AVFileType.Mpeg4;
        assetExport.OutputUrl = destination;
        assetExport.ShouldOptimizeForNetworkUse = true;
        assetExport.VideoComposition = videoComposition;

        assetExport.ExportAsynchronously(() =>
        {
            tcs.SetResult(assetExport.Status == AVAssetExportSessionStatus.Completed);
            switch (assetExport.Status)
            {
                case AVAssetExportSessionStatus.Completed:
                    // File.CoAVMutableVideoCompositionLayerInstructionpy(exportPath, des.RelativePath, true);
                    break;
                case AVAssetExportSessionStatus.Failed:
                    var e = assetExport.Error;
                    Console.WriteLine(e);
                    break;
                case AVAssetExportSessionStatus.Cancelled:

                    break;
            }
        });

        return tcs.Task;

    }

Что мне нужно, проверьте Mode (Portrait или Lanscape) и измените вращение. Может быть, мой код имеет проблему.

...