UIImagePickerController允许编辑= YES,修剪后获得未修剪的视频

时间:2012-09-03 14:35:15

标签: iphone ios ipad video uiimagepickercontroller

问题:

当我在我的UIImagePickerController中录制视频且allowEditing设置为YES,然后使用视频捕获后的trim-interface修剪视频时,我返回原始视频,而不是修剪过的视频。

设定:

我使用UIImagePickerController进行视频捕获,allowsEditing属性设置为YES。在委托方法didFinishPickingMediaWithInfo中,我使用信息UIImagePickerControllerMediaURL中的NSDictionary来获取路径网址。 The official Apple docs遗憾的是,未提及任何已修改的视频网址。

代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) {

        self.tempVideoPath = [[info objectForKey:
                                UIImagePickerControllerMediaURL] path];
    }
}

我意识到这个问题与此处发布的other ones类似,但没有明确的答案,为什么它不起作用或为什么选项就在那里。如果它是这样的,我不明白为什么选择器有'allowEditing'属性。

编辑:在我收到的信息词典中有以下键:

info: {
    UIImagePickerControllerMediaType = "public.movie";
    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/F12E4608-FE5A-4EE3-B4E2-8F7D2508C4C8/tmp/capture-T0x21d810.tmp.wabFCC/capturedvideo.MOV";
    "_UIImagePickerControllerVideoEditingEnd" = "5.498333333333333";
    "_UIImagePickerControllerVideoEditingStart" = "4.273402690887451";
}

这是否意味着我们必须自己修剪这些数据?那么Apple文档对此并不十分清楚。如果是这样,你知道一个好的做法吗?

3 个答案:

答案 0 :(得分:5)

看一下这篇文章中突出显示的答案:

How to trim the video using AVFoundation

我认为这正是你想要的。答案也使用了UIImagePickerController

希望它有所帮助, 的马里奥

答案 1 :(得分:1)

您需要使用UIVideoEditorController。它是Delegate Protocol指定方法videoEditorController:didSaveEditedVideoToPath:,它似乎是你想要的。 here中提供了可用的示例代码this SO question

答案 2 :(得分:1)

这是一个快速而肮脏的Swift 5示例,说明如何从UIImagePickerController剪裁视频

extension ViewController: UIImagePickerControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let mediaType = info[.mediaType] as! String

        dismiss(animated: true) { [weak self] in
            // Handle a movie capture
            if mediaType == kUTTypeMovie as String {
                guard let videoURL = info[.mediaURL] as? URL else {
                    SwiftyBeaver.error("Could not get URL for movie")
                    return
                }

                let editingEnd = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingEnd")
                let editingStart = UIImagePickerController.InfoKey(rawValue: "_UIImagePickerControllerVideoEditingStart")

                let startMilliseconds: Double?
                let endMilliseconds: Double?

                if let start = info[editingStart] as? Double, let end = info[editingEnd] as? Double {
                    startMilliseconds = start
                    endMilliseconds = end
                } else {
                    startMilliseconds = nil
                    endMilliseconds = nil
                }

                let alert = UIAlertController(title: "Creating", message: "File is being processed", preferredStyle: .alert)
                self?.present(alert, animated: true)
                self?.process(srcVideoURL: videoURL, startSeconds: startMilliseconds, endSeconds: endMilliseconds) { (error) in
                    DispatchQueue.main.async {
                        if let error = error {
                            alert.title = "Whoops"
                            alert.message = "\(error)"
                            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
                                self?.dismiss(animated: true, completion: nil)
                            }))
                            return
                        }
                        self?.dismiss(animated: true, completion: nil)
                    }
                }
            }
        }
    }
}

enum VideoError: Error {
    case error(message: String)
}

extension ViewController {
    func process(srcVideoURL: URL, startSeconds: Double?, endSeconds: Double?, completed: @escaping (_ error: Error?) -> ()) {
        DispatchQueue.global(qos: .userInitiated).async {
            let dstVideoURL: URL // some URL for the destination
            do {
                try self.handleNewVideo(srcVideoURL: srcVideoURL, dstVideoURL: dstVideoURL, startSeconds: startSeconds, endSeconds: endSeconds)
                completed(nil)
            } catch {
                completed(error)
            }
        }
    }

    func handleNewVideo(srcVideoURL: URL, dstVideoURL: URL, startSeconds: Double?, endSeconds: Double?) throws {
        guard let start = startSeconds, let end = endSeconds else {
            print("No video editing information. Copying file.")
            try FileManager.default.moveItem(at: srcVideoURL, to: dstVideoURL)
            return
        }

        print("Video editing information. Processing start \(start) end \(end).")
        let videoAsset = AVURLAsset(url: srcVideoURL)
        let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
        exportSession.outputURL = dstVideoURL
        exportSession.outputFileType = AVFileType.mov
        let timeRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), duration: CMTime(seconds: end - start, preferredTimescale: 1000))
        exportSession.timeRange = timeRange

        var error: Error? = nil
        let dispatchGroup = DispatchGroup()
        dispatchGroup.enter()
        exportSession.exportAsynchronously(completionHandler: {
            switch exportSession.status {
            case .completed:
                break
            case .failed:
                error = exportSession.error ?? VideoError.error(message: "Unknown failed error")
            case .cancelled:
                error = exportSession.error ?? VideoError.error(message: "Video Cancelled")
            case .exporting:
                error = exportSession.error ?? VideoError.error(message: "Video still exporting")
            case .unknown:
                error = exportSession.error ?? VideoError.error(message: "Unknown unknown error")
            case .waiting:
                error = exportSession.error ?? VideoError.error(message: "Waiting error")
            @unknown default:
                error = exportSession.error ?? VideoError.error(message: "Future error")
            }
            dispatchGroup.leave()
        })
        dispatchGroup.wait()

        if let error = error {
            throw error
        }
    }
}