Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 11-23-2010, 06:08 PM   #1 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default AVMutableComposition and MP4 - Removing Audio Track

I have yet to make heads or tails of this....I'm trying this code but I get nothing. Tried with .mp4 - no dice. Here trying with a .mov - no dice. The files are valid and do play on my ipad. Using the latest iPad OS.

Anybody? Apple's documentation leaves me a little mystified...

Thanks,
James A. Brannan


Code:
- (void) getAndPlayAVideoTest
{

AVMutableComposition *composition = [AVMutableComposition
          composition];

NSString *inputVideoPath = [[NSBundle mainBundle]
          pathForResource:@"Hawaii_bikeovision" ofType:@"mov"];

AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL 
              fileURLWithPath:inputVideoPath] options:nil];

AVMutableCompositionTrack *compositionVideoTrack = [composition 
            addMutableTrackWithMediaType:AVMediaTypeVideo
																				preferredTrackID:kCMPersistentTrackID_Invalid];
	
	
BOOL ok = NO;
	
AVAssetTrack * sourceVideoTrack = [[sourceAsset 
   tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
	
CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset 
  duration]);
	
ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack
   atTime:kCMTimeZero error:nil];
	
	
NSArray * paths2 =  
  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
  NSUserDomainMask, YES); 

NSString * tempS2 = [[paths2 objectAtIndex:0] 
  stringByAppendingPathComponent:@"Hawaii_no_sound.mov"];
	
if([[NSFileManager defaultManager] fileExistsAtPath:tempS2])
{
  [[NSFileManager defaultManager] removeItemAtPath:tempS2 error:nil];
}

	
NSURL *url = [[NSURL alloc] initFileURLWithPath: tempS2];
	
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] 
  initWithAsset:composition presetName:AVAssetExportPresetHighestQuality] 
  autorelease];

exporter.outputURL=url;
	
NSLog(@"%@", [exporter supportedFileTypes]);

exporter.outputFileType = @"com.apple.quicktime-movie";
[exporter exportAsynchronouslyWithCompletionHandler:^{
	}];
}
jamesbrannan is offline   Reply With Quote
Old 11-25-2010, 08:48 AM   #2 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default You answer this - I'll paypal you 200 dollars.

You get this working, and post to the forum, and I'll paypal you 200 dollars. It *MUST* work with my mp4 videos on my iPad running 4.2....

All I need to do is remove the audio from a video and then save the result. Forcing the user to do it on their desktop isn't an option...and I can't force online content developers to do it either. So I'm thinking of offering users an option to "batch remove" the audio...so they can "mix" their own with the video when watching. Remember, Apple - in it infinite wisdom - made video intrinsically tied with the System's Audio level.

James A. Brannan

Quote:
Originally Posted by jamesbrannan View Post
I have yet to make heads or tails of this....I'm trying this code but I get nothing. Tried with .mp4 - no dice. Here trying with a .mov - no dice. The files are valid and do play on my ipad. Using the latest iPad OS.

Anybody? Apple's documentation leaves me a little mystified...

Thanks,
James A. Brannan


Code:
- (void) getAndPlayAVideoTest
{

AVMutableComposition *composition = [AVMutableComposition
          composition];

NSString *inputVideoPath = [[NSBundle mainBundle]
          pathForResource:@"Hawaii_bikeovision" ofType:@"mov"];

AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL 
              fileURLWithPath:inputVideoPath] options:nil];

AVMutableCompositionTrack *compositionVideoTrack = [composition 
            addMutableTrackWithMediaType:AVMediaTypeVideo
																				preferredTrackID:kCMPersistentTrackID_Invalid];
	
	
BOOL ok = NO;
	
AVAssetTrack * sourceVideoTrack = [[sourceAsset 
   tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
	
CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset 
  duration]);
	
ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack
   atTime:kCMTimeZero error:nil];
	
	
NSArray * paths2 =  
  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
  NSUserDomainMask, YES); 

NSString * tempS2 = [[paths2 objectAtIndex:0] 
  stringByAppendingPathComponent:@"Hawaii_no_sound.mov"];
	
if([[NSFileManager defaultManager] fileExistsAtPath:tempS2])
{
  [[NSFileManager defaultManager] removeItemAtPath:tempS2 error:nil];
}

	
NSURL *url = [[NSURL alloc] initFileURLWithPath: tempS2];
	
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] 
  initWithAsset:composition presetName:AVAssetExportPresetHighestQuality] 
  autorelease];

exporter.outputURL=url;
	
NSLog(@"%@", [exporter supportedFileTypes]);

exporter.outputFileType = @"com.apple.quicktime-movie";
[exporter exportAsynchronouslyWithCompletionHandler:^{
	}];
}
jamesbrannan is offline   Reply With Quote
Old 11-25-2010, 07:11 PM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 247
linkmx is on a distinguished road
Default

The problem is that AVAssetExportSession is releasing before starting the exporting proccess, if you remove the autorelease like this:

Code:
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
  initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
The video exports fine and without audio. What you have to do is release the exporter when the process is done.

You can do it with the following code:

Code:
	
	[exporter exportAsynchronouslyWithCompletionHandler:^{[exporter release];
	}];

Last edited by linkmx; 11-25-2010 at 07:15 PM.
linkmx is offline   Reply With Quote
Old 11-25-2010, 10:35 PM   #4 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default

I will test this out on Friday. If it works, cool. otherwise I'm just going to disable the audio track on playback. Seems AvFoundation does allow muting a videos audio. Not sure if that will mute an iPod audio resource too by disabling a videos audio track though (stranger things have happened). but, if your right and that is all it was then I'll honor my offer.

James

Quote:
Originally Posted by linkmx View Post
The problem is that AVAssetExportSession is releasing before starting the exporting proccess, if you remove the autorelease like this:

Code:
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
  initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
The video exports fine and without audio. What you have to do is release the exporter when the process is done.

You can do it with the following code:

Code:
	
	[exporter exportAsynchronouslyWithCompletionHandler:^{[exporter release];
	}];
jamesbrannan is offline   Reply With Quote
Old 11-25-2010, 10:43 PM   #5 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default

Please note, if the one answer posted works I will honor the offer and will pay the guy, otherwise, note that I figured out what I think is a better solution, which I will post. It seems that AVFoundation can do a heck of a lot, even play a video with the audio track disabled! This was not possible using the media framework. so no answers needed.

James



Quote:
Originally Posted by jamesbrannan View Post
You get this working, and post to the forum, and I'll paypal you 200 dollars. It *MUST* work with my mp4 videos on my iPad running 4.2....

All I need to do is remove the audio from a video and then save the result. Forcing the user to do it on their desktop isn't an option...and I can't force online content developers to do it either. So I'm thinking of offering users an option to "batch remove" the audio...so they can "mix" their own with the video when watching. Remember, Apple - in it infinite wisdom - made video intrinsically tied with the System's Audio level.

James A. Brannan
jamesbrannan is offline   Reply With Quote
Old 11-25-2010, 11:16 PM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 247
linkmx is on a distinguished road
Default

Yes, with AVPlayer you can disable the different tracks of a video, so if you want to play just the video without audio you can disable the audio tracks.
linkmx is offline   Reply With Quote
Old 11-26-2010, 07:12 AM   #7 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default

Quote:
Originally Posted by linkmx View Post
Yes, with AVPlayer you can disable the different tracks of a video, so if you want to play just the video without audio you can disable the audio tracks.
You sir are the proud recipient of 200 paypal. I'll send you an offline message to get your info. Lest someone ridicules me...remember, development is expensive...I recognize the value of a timely answer. I was so caught up in it not working, I overlooked the most basic of error...

However, I am RTFMing the AVFoundation stuff, and I'm thinking I'm going to remove the audio track instead and use it for playing. Besides, its got all this time-based stuff that I could use with a future API of the device that I'm working with. linkmx, I reserve the right to ask you one or two question on the playback if I need too, cool?

Thanks,
James A. Brannan
James A. Brannan's Site on all Things Programming, iPhone,
jamesbrannan is offline   Reply With Quote
Old 11-26-2010, 07:18 AM   #8 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default

Here's the code that works....not production quality, no error handling etc. This AVFoundation stuff is cool. BTW, there are two WWDC10 videos on it that you can access via Apple Developer Connection. I'll post stuff on my playback as I get it done/working, as there is not much info on this, and a TON of posts on the web that predate 4.0 that basically say "mute a video, can't do it...."

AVFoundation is cool as ****

James A. Brannan
James A. Brannan's Site on all Things Programming, iPhone,

Code:
- (void) getAndPlayAVideoTest
{
	AVMutableComposition *composition = [AVMutableComposition composition];
	
NSString *inputVideoPath = [[NSBundle mainBundle]
	pathForResource:@"Nicasio Loop Sample - Americas Best Rides" ofType:@"mp4"];
	
AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL 
  fileURLWithPath:inputVideoPath] options:nil];
	
AVMutableCompositionTrack *compositionVideoTrack = [composition 
   addMutableTrackWithMediaType:AVMediaTypeVideo
													preferredTrackID:kCMPersistentTrackID_Invalid];
	
BOOL ok = NO;
	
AVAssetTrack * sourceVideoTrack = [[sourceAsset 
   tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
	
CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset
   duration]);
	
ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack
	 atTime:kCMTimeZero error:nil];
	
	
NSArray * paths2 =  
   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
   NSUserDomainMask, YES); 
	
NSString * tempS2 = [[paths2 objectAtIndex:0] 
						 stringByAppendingPathComponent:@"Hawaii_no_sound.mp4"];
	
if([[NSFileManager defaultManager] fileExistsAtPath:tempS2])
{
   [[NSFileManager defaultManager] removeItemAtPath:tempS2 error:nil];
}
	
	
NSURL *url = [[NSURL alloc] initFileURLWithPath: tempS2];
	
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
									   initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
	
exporter.outputURL=url;
	
NSLog(@"%@", [exporter supportedFileTypes]);
	
exporter.outputFileType = @"com.apple.quicktime-movie";
	
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
	DLog(@"XXXXXXXXXXXXXXXXXXXXXXXXX I'M DONE");
	[exporter release];
}];
}
jamesbrannan is offline   Reply With Quote
Old 12-03-2010, 10:31 PM   #9 (permalink)
New User
 
jamesbrannan's Avatar
 
Join Date: Aug 2008
Location: Gaithersburg, MD
Posts: 178
jamesbrannan is on a distinguished road
Default

I should mention, though, this is *very* slow as the file size gets large, I suppose as would be expected. But see my other thread, simply setting enabled to false for the audio track is *very* quick and easy.

Quote:
Originally Posted by jamesbrannan View Post
Here's the code that works....not production quality, no error handling etc. This AVFoundation stuff is cool. BTW, there are two WWDC10 videos on it that you can access via Apple Developer Connection. I'll post stuff on my playback as I get it done/working, as there is not much info on this, and a TON of posts on the web that predate 4.0 that basically say "mute a video, can't do it...."

AVFoundation is cool as ****

James A. Brannan
James A. Brannan's Site on all Things Programming, iPhone,

Code:
- (void) getAndPlayAVideoTest
{
	AVMutableComposition *composition = [AVMutableComposition composition];
	
NSString *inputVideoPath = [[NSBundle mainBundle]
	pathForResource:@"Nicasio Loop Sample - Americas Best Rides" ofType:@"mp4"];
	
AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL 
  fileURLWithPath:inputVideoPath] options:nil];
	
AVMutableCompositionTrack *compositionVideoTrack = [composition 
   addMutableTrackWithMediaType:AVMediaTypeVideo
													preferredTrackID:kCMPersistentTrackID_Invalid];
	
BOOL ok = NO;
	
AVAssetTrack * sourceVideoTrack = [[sourceAsset 
   tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
	
CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset
   duration]);
	
ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack
	 atTime:kCMTimeZero error:nil];
	
	
NSArray * paths2 =  
   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
   NSUserDomainMask, YES); 
	
NSString * tempS2 = [[paths2 objectAtIndex:0] 
						 stringByAppendingPathComponent:@"Hawaii_no_sound.mp4"];
	
if([[NSFileManager defaultManager] fileExistsAtPath:tempS2])
{
   [[NSFileManager defaultManager] removeItemAtPath:tempS2 error:nil];
}
	
	
NSURL *url = [[NSURL alloc] initFileURLWithPath: tempS2];
	
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
									   initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
	
exporter.outputURL=url;
	
NSLog(@"%@", [exporter supportedFileTypes]);
	
exporter.outputFileType = @"com.apple.quicktime-movie";
	
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
	DLog(@"XXXXXXXXXXXXXXXXXXXXXXXXX I'M DONE");
	[exporter release];
}];
}
jamesbrannan is offline   Reply With Quote
Old 12-04-2010, 09:23 AM   #10 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 236
lilzz is on a distinguished road
Default

james,

how would you do it if you try to insert the same video again to the mix composition 10s after your first insertion?

do you create another track and add to the composition or keep the same track but with the added delays?
lilzz is offline   Reply With Quote
Old 07-28-2011, 08:59 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 4
zhanglei is on a distinguished road
Default

Quote:
Originally Posted by jamesbrannan View Post
I should mention, though, this is *very* slow as the file size gets large, I suppose as would be expected. But see my other thread, simply setting enabled to false for the audio track is *very* quick and easy.
James, i set enabled to false, but it still can play the audio.
zhanglei is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 377
16 members and 361 guests
Brandt, coolman, fredidf, Free App Monster, givensur, iAppDeveloper, jbro, Kryckter, locombiano89, Mah6447, Meoz, simplymuzik3, stanny, stevenkik, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0