(Graphic Images, animations, multimedia) Reading Notes-recording and editing videos

Source: Internet
Author: User

(Graphic Images, animations, multimedia) Reading Notes-recording and editing videos

Use UIImagePickerController for recording

 

# Import ViewController. h # import
 
  
# Import
  
   
@ Interface ViewController ()
   
    
-(IBAction) videoRecod :( id) sender; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(IBAction) videoRecod :( id) sender {if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerCo NtrollerSourceTypeCamera]) {UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController. delegate = self; imagePickerController. sourceType = UIImagePickerControllerSourceTypeCamera; imagePickerController. mediaTypes = [[NSArray alloc] initWithObjects :( NSString *) kUTTypeMovie, nil]; // sets the recording quality to imagePickerController. videoQuality = UIImagePickerControll ErQualityTypeHigh; // only 30 seconds at most can be recorded. imagePickerController. videoMaximumDuration = 30366f; [self presentViewController: imagePickerController animated: YES completion: nil];} else {NSLog (@ camera unavailable .); }-(Void) handle: (UIImagePickerController *) picker {[self dismissViewControllerAnimated: YES completion: nil];}-(void) imagePickerController: (UIImagePickerController *) picker handle: (NSDictionary *) info {NSURL * url = [info objectForKey: UIImagePickerControllerMediaURL]; NSString * tempFilePath = [url path]; if (UIVideoAtPathIsCompatibleWithSave DPhotosAlbum (tempFilePath) {// video: didFinishSavingWithError: contextInfo: Must be saved as the callback class (tempFilePath, self, @ selector (video: didFinishSavingWithError: contextInfo :), (_ bridge void *) (tempFilePath);} [self dismissViewControllerAnimated: YES completion: nil];}-(void) video :( NSString *) videoPath didFinishSavingWithError :( NSError *) error contextInfo :( NSString *) cont ExtInfo {NSString * title; NSString * message; if (! Error) {title = @ save video; message = @ the video has been saved to the camera film of the device;} else {title = @ video failed; message = [error description];} UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: message delegate: nil cancelButtonTitle: @ OK otherButtonTitles: nil]; [alert show];}-(void) navigationController :( UINavigationController *) navigationController willShowViewController :( UIViewController *) viewController animate D :( BOOL) animated {NSLog (@ selector will be displayed .);} -(Void) navigationController :( UINavigationController *) navigationController didShowViewController :( UIViewController *) viewController animated :( BOOL) animated {NSLog (@ selector display ends .);}
   
  
 

Use AVFoundation

 

 

AVCaptureSession: capture sessions to capture data from the camera and microphone. The AVCaptureSession object must be used to coordinate input and output data.

AVCaptureDevice: capture device, which indicates entering a device, such as a camera and a microphone.

AVCaptureDeviceInput: capture an input data source of a session

AVCaptureOutput captures an output target of a session, such as the output video file and static image.

AVCaptureMovieFileOutput is a subclass of AVCaptureOutput. It can be used to output captured data to a QuickTime video file (MOV)

AVCaptureVideoPreviewLayer is a subclass of CALayer that can be used to display recorded videos.

AVCaptureConnection: capture a connection. It is a connection between input and output in a capture session.

 

 

# Import ViewController. h # import @ interface ViewController () vcapturefileoutputrecordingdelegate> {BOOL isRecording;} @ property (weak, nonatomic) IBOutlet UILabel * label; @ property (weak, nonatomic) IBOutlet UIButton * button; @ property (strong, nonatomic) AVCaptureSession * session; @ property (strong, nonatomic) AVCaptureMovieFileOutput * output;-(IBAction) recordPressed :( id) sender; @ end @ implement Ation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. session = [[AVCaptureSession alloc] init]; self. session. sessionPreset = camera; AVCaptureDevice * cameraDevice = [AVCaptureDevice failed: camera]; NSError * error = nil; camera * camera = [deviceInputWithDevice: cameraDevice error: & error]; AVCaptureDe Vice * micDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio]; AVCaptureDeviceInput * mic = [AVCaptureDeviceInput deviceInputWithDevice: micDevice error: & error]; if (error |! Camera |! Mic) {NSLog (@ Input Error);} else {// Add the captured audio and video [self. session addInput: camera]; [self. session addInput: mic];} self. output = [[AVCaptureMovieFileOutput alloc] init]; if ([self. session canAddOutput: self. output]) {[self. session addOutput: self. output]; // output} AVCaptureVideoPreviewLayer * previewLayer = [AVCaptureVideoPreviewLayer layerWithSession: self. session]; previewLayer. frame = CGRectMake (0, 0, self. View. frame. size. width, self. view. frame. size. height); [self. view. layer insertSublayer: previewLayer atIndex: 0]; [self. session startRunning]; isRecording = NO; self. label. text = @;}-(void) didreceivemorywarning {[super didreceivemorywarning];}-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; if (! [Self. session isRunning]) {[self. session startRunning] ;}}- (void) viewWillDisappear :( BOOL) animated {[super viewWillDisappear: animated]; if ([self. session isRunning]) {[self. session stopRunning] ;}}- (IBAction) recordPressed :( id) sender {if (! IsRecording) {[self. button setTitle: @ stop forState: UIControlStateNormal]; self. label. text = @ recording ...; isRecording = YES; NSURL * fileURL = [self fileURL]; [self. output startRecordingToOutputFileURL: fileURL recordingDelegate: self];} else {[self. button setTitle: @ recording forState: UIControlStateNormal]; self. label. text = @ stop; [self. output stopRecording]; isRecording = NO;}-(NSURL *) fileURL {NSString * out PutPath = [[NSString alloc] initWithFormat: %%@, NSTemporaryDirectory (), @ movie. mov]; NSURL * outputURL = [[NSURL alloc] initFileURLWithPath: outputPath]; NSFileManager * manager = [[NSFileManager alloc] init]; if ([manager fileExistsAtPath: outputPath]) {[manager removeItemAtPath: outputPath error: nil];} return outputURL;} # pragma mark -- AVCaptureFileOutputRecordingDelegate delegate protocol implementation method-(void) captureOut Put :( AVCaptureFileOutput *) captureOutput didFinishRecordingToOutputFileAtURL :( NSURL *) outputFileURL fromConnections :( NSArray *) connections error :( NSError *) error {if (error = nil) {ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; [library writeVideoAtPathToSavedPhotosAlbum: outputFileURL completionBlock: ^ (NSURL * assetURL, NSError * error) {if (error) {NSLog (@ write error .) ;}}] ;}}


 

Use UIVideoEditorController

 

# Import ViewController. h @ interface ViewController ()
 
  
-(IBAction) editButtonPress :( id) sender; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(IBAction) editButtonPress :( id) sender {NSBundle * bundle = [NSBundle mainBundle]; NSString * movie Path = [bundle pathForResource: @ YY ofType: @ mp4]; // determines whether the device supports video editing if ([UIVideoEditorController canEditVideoAtPath: moviePath]) {UIVideoEditorController * videoEditor = [[UIVideoEditorController alloc] init]; videoEditor. delegate = self; videoEditor. videoPath = moviePath; [self presentViewController: videoEditor animated: YES completion: NULL];} else {NSLog (@ cannot edit this video);}-(void) videoEditorController :( UIVideoEditorController *) editor identifier :( NSString *) editedVideoPath {[editor dismissViewControllerAnimated: YES completion: NULL]; if (Response (editedVideoPath) {response (editedVideoPath, self, @ selector (video: didFinishSavingWithError: contextInfo :), (_ bridge void *) (editedVideoPath) ;}}-(void) videoEditorControll Er :( UIVideoEditorController *) editor didFailWithError :( NSError *) error {NSLog (@ error in Video editing); NSLog (@ Video editor error occurred =%@, error); [editor dismissViewControllerAnimated: YES completion: NULL];}-(void) videoEditorControllerDidCancel :( UIVideoEditorController *) editor {NSLog (@ video edit canceled); [editor dismissViewControllerAnimated: YES completion: NULL];} -(void) video :( NSString *) videoPathdidFinishSavingWit HError :( NSError *) error contextInfo :( NSString *) contextInfo {NSString * title; NSString * message; if (! Error) {title = @ save video; message = @ the video has been saved to the camera film of the device;} else {title = @ video failed; message = [error description];} UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: message delegate: nil cancelButtonTitle: @ OK otherButtonTitles: nil]; [alert show];}
 

My personal comments: in fact, I have done video before, and there are similar articles. I can find better and more technical Code and articles on some foreign websites, but in short, it's a learning record ~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.