Trimming video with Monotouch fails with “The operation could not be completed”

后端 未结 1 680
清歌不尽
清歌不尽 2021-01-26 16:34

I am trying to trim a video to 5 seconds programmatically. Here is my implementation.

AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,AVA         


        
1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 17:18

    Try this code below. I modified exportSession.OutputUrl and how you initialize your CMTimeRange. Are you trimming it down to a 4 second clip?

    var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
    var preset="";
    
    if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
    {
        preset="AVAssetExportPresetLowQuality";
    }
    else
    {
        preset=compatiblePresets.FirstOrDefault();
    }
    
    using (var exportSession = new AVAssetExportSession(videoAsset, preset))
    {
        int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
        string filename;
        if (SystemVersion >= 8)
        {
            var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
            filename = Path.Combine(documents, "trimmed.mov");
        }
        else
        {
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
            filename = Path.Combine(documents, "trimmed.mov");
        }
    
        exportSession.OutputUrl = NSUrl.FromFilename(filename);
        exportSession.OutputFileType = AVFileType.QuickTimeMovie;
    
        var range = new CMTimeRange();
        range.Start = CMTime.FromSeconds (1, videoAsset.Duration.TimeScale);
        range.Duration = CMTime.FromSeconds (5, videoAsset.Duration.TimeScale);
        exportSession.TimeRange = range;
    }
    
    ExportTrimmedVideo( exportSession);
    

    0 讨论(0)
提交回复
热议问题