使用Monotouch修剪视频失败,并显示“操作无法完成". [英] Trimming video with Monotouch fails with "The operation could not be completed"

查看:137
本文介绍了使用Monotouch修剪视频失败,并显示“操作无法完成".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式将视频修剪到5秒.这是我的实现.

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

AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,AVAssetExportSession.PresetLowQuality.ToString());
            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");
            }
            outputUrl=new NSUrl(filename);
            exportSession.OutputUrl = outputUrl;

            CMTime start = new CMTime((long)1, 1);

            CMTime duration = new CMTime((long)5, 1);

            CMTimeRange range = new CMTimeRange();
            range.Start=start;
            range.Duration=duration;
            exportSession.TimeRange = range;

            exportSession.OutputFileType = AVFileType.QuickTimeMovie;
            ExportTrimmedVideo( exportSession);
async void ExportTrimmedVideo(AVAssetExportSession exportSession)
    {
        await exportSession.ExportTaskAsync ();
        if (exportSession.Status == AVAssetExportSessionStatus.Completed) {
            InvokeOnMainThread (() => {
                new UIAlertView ("Export Sucess", "Video is trimmed", null, "O K").Show ();
            });
        }
        else 
        {
            InvokeOnMainThread (() => {
                new UIAlertView ("Export Falure", exportSession.Error.Description, null, "O K").Show ();
            });
        }
}

但是在完成过程中,我获得了提起诉讼的状态.完整的NSError描述如下

But in completion I am getting a Filed Status. Full NSError Description is as follows

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x7cebcf80 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x7cb08410 "The operation couldn’t be completed. (OSStatus error -12105.)", NSLocalizedFailureReason=An unknown error occurred (-12105)}

我可能做错了什么?

我已经引用了苹果的文档在修剪视频上,并修改了上面的代码,但没有产生任何积极效果,如下所示.

I have referred apple's documentation on Trimming video and have modified the above code with no positive effect as below.

var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
            var preset="";
            if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
            {
                preset="AVAssetExportPresetLowQuality";
            }
            else
            {
                preset=compatiblePresets.FirstOrDefault();
            }
            AVAssetExportSession 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");
            }
            outputUrl=new NSUrl(filename);
            exportSession.OutputUrl = outputUrl;
            exportSession.OutputFileType = AVFileType.QuickTimeMovie;
            CMTime start = new CMTime((long)1, 600);

            CMTime duration = new CMTime((long)5, 600);

            CMTimeRange range = new CMTimeRange();
            range.Start=start;
            range.Duration=duration;
            exportSession.TimeRange = range;


            ExportTrimmedVideo( exportSession);

推荐答案

请在下面尝试此代码.我修改了exportSession.OutputUrl以及如何初始化CMTimeRange.您正在将其修剪到4秒的剪辑吗?

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);

这篇关于使用Monotouch修剪视频失败,并显示“操作无法完成".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆