Is it possible to programmatically create video frame-by-frame in iOS? - iphone

I want to make an app where users can create funny stick figure animations.
It would be cool if it is possible to export them as video. Can I "draw" video frames frame by frame and render them into a H.264 or other video format?
The length will be between 2 seconds and 5 minutes. I heared a while back that there is a framework to edit video but in my case I really need to create a video from scratch. What are my options?

You might need to use a multimedia framework which provides more lower level control, like gstreamer or ffmeg.
Alternately, you can create an MJPEG and figure out a way to transcode it.

Yes, you can examine :
CEMovieMaker
Usage:
UIImage *frameImg = <Some Image>;
NSDictionary *settings = [CEMovieMaker videoSettingsWithCodec:AVVideoCodecTypeH264
withWidth:source.size.width
andHeight:source.size.height
];
///
CEMovieMaker * movieMaker = [[CEMovieMaker alloc] initWithSettings:settings];
/// Complete video
[movieMaker createMovieFromImages:[self.movieImages copy] withCompletion:^(NSURL *fileURL){
//AVPlayerViewController or
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[playerController.view setFrame:self.view.bounds];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer play];
[self.view addSubview:playerController.view];
}];

Related

UIVideoEditorController lost video resolution

I am trying to use UIVideoEditorController to edit my video, but it seems to lose my video resolution. My original video was 720 x 1280, but after using the UIVideoEditorController, the quality becomes 360 x 640.
I tried to set the videoQuality to be UIImagePickerControllerQualityTypeHigh or even UIImagePickerControllerQualityTypeIFrame1280x720, but that doesn't help.
I am working on the iPad and here are my code:
self.editorController = [[[UIVideoEditorController alloc] init] autorelease];
self.editorController.videoPath = self.tempVideoPath;
self.editorController.delegate = self;
self.editorController.videoQuality = UIImagePickerControllerQualityTypeHigh;
CKLog(#"%d", self.editorController.videoQuality);
self.popOverController = [[[UIPopoverController alloc] initWithContentViewController:self.editorController] autorelease];
self.popOverController.delegate = self;
self.popOverController.popoverContentSize = CGSizeMake(700, 700);
[self.popOverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.videoView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
According to the UIVideoEditorController documentation:
A UIVideoEditorController object, or video editor, manages the
system-supplied user interface for trimming video frames from the
start and end of a previously recorded movie as well as reencoding to
lower quality...
The UIVideoEditorController class has a property called videoQuality. It allows us to choose the video quality when saving the trimmed video. (The default value is UIImagePickerControllerQualityTypeLow)
UIImagePickerControllerQualityTypeHigh uses the highest-quality video recording supported for the active camera on the device. It looks much better, but still seems to lose a little of the original resolution.

AVPlayer Questions, while Live Streaming (iOS)

I have AVPlayer Questions.
1.How to control the volume of it?
2.How to know if the AVPlayer is reloading music because bad connection, do i have some inidication of it?
AVPlayer uses the system volume, so if you need to provide controls for this you can use MPVolumeView which gives you the slider for volume control.
For audio fading, you can use an AVAudioMix. Here's some code:
//given an AVAsset called asset...
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
id audioMix = [[AVAudioMix alloc] init];
id volumeMixInput = [[AVMutableAudioMixInputParameters alloc] init];
//fade volume from muted to full over a period of 3 seconds
[volumeMixInput setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))];
[volumeMixnput setTrackID:[[asset tracks:objectAtIndex:0] trackID]];
[audioMix setInputParameters:[NSArray arrayWithObject:volumeMixInput]];
[playerItem setAudioMix:audioMix];
You can also abruptly set the volume for a mix at a given time with:
[volumeMixInput setVolume:.5 atTime:CMTimeMakeWithSeconds(15, 1)];
Hope this helps. This API is definitely not obvious. I'd highly recommend watching the WWDC 10 video entitled Discovering AV Foundation. It's excellent.

Adding video files in iPhone

I am new to iPhone. I need sample code for how to add video files in iPhone.
Include the media player framework then do this:
MPMoviePlayerViewController *mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://mywebsite.com/movie.mov"]]; // this can be an NSURL to a file in your bundle
[self presentMoviePlayerViewControllerAnimated:mediaPlayer];
mediaPlayer.view.backgroundColor = [UIColor blackColor];
[mediaPlayer release];
there are notifications and callbacks, but this is the basics.
you can find basically all the things you need here --> http://pdfcast.org/pdf/the-objective-c-programming-language ... download the pdf... enjoy!

Writing an app to stream video to iPhone

I'm interested in creating an iPhone app that can stream video from a central server, YouTube style. I was wondering if anyone has ever tried to do this before, what is the path of least resistant, existing APIs, etc? I really know nothing about how this is generally done. Would I be working with sockets? Just looking for some direction here. Thanks!
If you have the streaming server up and ready, it is quite easy to implement a video controller that pops up youtube-style.
NSString *videoURLString = #"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
You need to handle the controller that display the video player's view (which is self in this case).
In iOS 3.2+ MPMoviePlayerViewController make it even easier:
NSString *videoURLString = #"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerViewController *moviePlayerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:moviePlayerView];
presentMoviePlayerViewControllerAnimated is a MediaPlayer's additional method to FWViewController that you will find in iOS 3.2+ and it takes care of creating a view controller and pushing it on the stack, animating it with a slide-from-bottom animation, as in youtube.app.
Apple has a detailed article about setting up server side for media streaming:
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/Introduction/Introduction.html
and Best Practices Note:
https://developer.apple.com/library/content/technotes/tn2224/_index.html
Not only it contains info about streaming service architecture and tools used to build it but also has some requirements to such kind of service that must be fulfilled and references to live test streams.
Use this code to use low memory. On streaming video....
-(IBAction)playMovie:(NSURL *) theURL
{
NSURL *fileURL = theURL;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
QuickTime videos already stream to the phone. The path of least resistance would be to use the media player controller and point it to a streaming media file on a streaming server.
While the existing answers are good, if you need to use non HTTP streams (mms or rtmp for example) or non Apple supported audio / video codecs, things get a bit more complicated.
I'm not an expert myself, but I've been using this VideoStreaming SDK to solve those problems, and it makes customizing the client much easier (background streaming, pausing streams, etc). Might be worth a look if you have those requirements as well.
2018 answer You can use AVPlayerViewController since MPMoviePlayerController is deprecated since iOS 9
NSURL *url = [NSURL URLWithString:videoUrl];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:url];
_playerViewController.player.volume = 1;
_playerViewController.showsPlaybackControls = YES;
_playerViewController.view.frame = CGRectMake(....);
[self.view addSubview:_playerViewController.view];

Embedded Video in a UIView with iPhone

I would like to write an app that downloads (or streams) a video (encoded as required) in a view. I dont want to use the MPVideoPlayer from the SDK as it opens the video in full screen. I would like to place another UIView (transparent) over the video so that my users can annotate over the video.
Anyone have any idea or can point me to some code that will play video in a UIView?
If you want to do this you will need to include your own (software) video decoder, which will not have access to the hardware acceleration on the system. Even if you can get it working with acceptable performance it will be a huge battery drain.
If you wish to play a video in portrait mode, I have solution for that.
If you think that -MPMovie Player can run under a view, according to me it's not possible.
MP Movie player will work as Apple has designed.
So, MP Movie player will always / almost run in full screen.
Solution for portrait mode.
#interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
#end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
Hope that will help to you.
See, My question is very similar like yours.
playing video in custom size screen - view in iphone
Try this:
UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor orangeColor];
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(#"cannot find %# in bundle or doctuments", path);
}
NSURL *url = [NSURL fileURLWithPath:path];
MoviePlayerViewController *mpvc = [[MoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpvc.moviePlayer];
mpvc.moviePlayer.fullscreen = NO;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleNone];
mpvc.moviePlayer.view.frame = CGRectMake(10, 100, 300, 300);
[v.view addSubview:mpvc.moviePlayer.view];
[mpvc.moviePlayer play];
[self presentModalViewController:v animated:YES];
[v release];
Maybe you should check the MediaPlayer's private headers, and simply add the video's view as your view's subview.