iOS - 使用蘋果自帶的UIVideoEditController進行視頻編輯


 

UIVideoEditorController類包含了由系統提供的界面,使用戶可以交互式的剪切視頻。UIVideoEditorController對象處理用戶的交互並且提供把編輯后的視頻的文件系統路徑提供給UIVideoEditorControllerDelegate對象.

UIVideoEditorController只支持能夠支持視頻編輯的設備. 
我們設置好它的delegate及videoPath屬性,並將其展示出來。(經過videoQuality屬性,也可以通過這個類將視頻重新編碼成質量較低的格式)

 

@property(nullable, nonatomic,assign) id <UINavigationControllerDelegate, UIVideoEditorControllerDelegate> delegate; @property(nonatomic, copy) NSString *videoPath; // 視頻路徑

@property(nonatomic) NSTimeInterval videoMaximumDuration; // default value is 10 minutes. set to 0 to specify no maximum duration. @property(nonatomic) UIImagePickerControllerQualityType videoQuality; // default value is UIImagePickerControllerQualityTypeMedium

 

 

UIVideoEditorController和UIImagePickerController的主要區別是前者能提供視頻的編輯,后者主要用於錄像或者視頻的選擇. 
UIVideoEditorController視頻編輯器所用的delegate回調與UIImagePickerController類相似,這些回調方法分別來處理成功、失敗、取消這三種情況:

videoEditorController:didSaveEditedVideoToPath:

videoEditorController: didFailWithError:

videoEditorControllerDidCancel:

 

 

#import <AVFoundation/AVFoundation.h>

#import "ViewController.h"

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIVideoEditorControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

     [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

      [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.

}

- (IBAction)click:(UIButton *)sender {

      UIImagePickerController *myImagePickerController = [[UIImagePickerController alloc] init];

      myImagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

      myImagePickerController.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:myImagePickerController.sourceType];

      myImagePickerController.delegate = self; myImagePickerController.editing = NO;

      [self presentViewController:myImagePickerController animated:YES completion:nil];

}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

      [picker dismissViewControllerAnimated:YES completion:^{

              NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

              if([mediaType isEqualToString:@"public.movie"]) {

                     NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

                     UIVideoEditorController *editVC; // 檢查這個視頻資源能不能被修改

                    if ([UIVideoEditorController canEditVideoAtPath:videoURL.path]) {

                            editVC = [[UIVideoEditorController alloc] init];

                            editVC.videoPath = videoURL.path;

                            editVC.delegate = self;

                    }

                   [self presentViewController:editVC animated:YES completion:nil];

                }

       }];

}

//編輯成功后的Video被保存在沙盒的臨時目錄中

- (void)videoEditorController:(UIVideoEditorController *)editor didSaveEditedVideoToPath:(NSString *)editedVideoPath {

       NSLog(@"+++++++++++++++%@",editedVideoPath);

}
// 編輯失敗后調用的方法

- (void)videoEditorController:(UIVideoEditorController *)editor didFailWithError:(NSError *)error {

       NSLog(@"%@",error.description);

}

//編輯取消后調用的方法

- (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor {}

@end

 

 

這是swift版本:

class ViewController: UIViewController, UIVideoEditorControllerDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

     var editVideoViewController:UIVideoEditorController!

     @IBAction func editVideoTapped(sender: AnyObject) {

          let imagePicker = UIImagePickerController()

          imagePicker.delegate = self

          imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

          let types = UIImagePickerController.availableMediaTypesForSourceType(.Camera)!

          imagePicker.mediaTypes = [types[1]]//kUTTypeMovie

          self.presentViewController(imagePicker, animated: true, completion: nil)

}

//MARK: - UIImagePickerControllerDelegate

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        picker.dismissViewControllerAnimated(true) { () -> Void in

             let mediaType:String = info[UIImagePickerControllerMediaType] as! String

             if mediaType == "public.movie" {

                  let url: NSURL = info[UIImagePickerControllerMediaURL] as! NSURL

                  let editVideoViewController = UIVideoEditorController()

                 //設置delegate

                editVideoViewController.delegate = self

                //設置要編輯的視頻地址

                editVideoViewController.videoPath = videoPath!

                self.presentViewController(editVideoViewController, animated: true, completion: nil)

              }

          }

}

//MARK: - UIVideoEditorControllerDelegate

//編輯成功后的Video被保存在沙盒的臨時目錄中

func videoEditorController(editor: UIVideoEditorController, didSaveEditedVideoToPath editedVideoPath: String) {

        print("editedVideopath = \(editedVideoPath)")

        dismissViewControllerAnimated(true, completion: {})

}

//編輯失敗后調用的方法

func videoEditorController(editor: UIVideoEditorController, didFailWithError error: NSError) {

         print("error=\(error.description)")

         dismissViewControllerAnimated(true, completion: {})

}

//編輯取消后調用的方法

func videoEditorControllerDidCancel(editor: UIVideoEditorController) {

         dismissViewControllerAnimated(true, completion: {})

       }

}

 

 

這是效果圖 :

這里寫圖片描述

使用UIVideoEditController進行視頻編輯是基於蘋果封裝好的編輯類,要是我們實現自定義視頻截取,就要自己寫了,在這里分享一個視頻編輯並裁剪的一個第三方.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM