How to Trim Audio File From Specified portion in iphone - ios

float vocalStartMarker = 1.0;
float vocalEndMarker = 3.0;
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSURL *audioFileInput =[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/idea_honey_bunny.mp3", [[NSBundle mainBundle] resourcePath]]];
NSURL *audioFileOutput =[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/idea_honey_bunny.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
if (!audioFileInput || !audioFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:audioFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetAppleM4A];
if (exportSession == nil)
{
return NO;
}
CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 1)), 1);
CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 1)), 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"It worked!");
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
}
}];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileOutput error:&error];
// audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(#"%#",error);
else
[audioPlayer play];
return YES;
}
i want to try trimming audio file but compiler not entered in this if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"It worked!");
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
}
}]; bloock and file not trim

You have given same path for audioFileOutput and audioFileInput...So it is going into fail case.And also make sure that file is existed at input path...please change the OutputFile path and check..

exportAsynchronouslyWithCompletionHandler method takes some time to export new asset. So you create your audio player with asset, which not ready.
Try to change your code like this
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"It worked!");
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileOutput error:&error];
if (audioPlayer == nil) {
NSLog(#"%#",error);
}
else {
[audioPlayer play];
}
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
}
}];
Also, as Murali said above, change path for output file

Related

+ exportSessionWithAsset:presetName: returning nil on iOS7

I'm using the following code:
- (AVAssetExportSession *)testRecording
{
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:kTestSongName withExtension:#"mp3"];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTimeRange range = CMTimeRangeMake(CMTimeMake(0, 600), asset.duration);
NSError *editError;
BOOL result = [composition insertTimeRange:range ofAsset:asset atTime:kCMTimeZero error:&editError];
if (!result) {
NSLog(#"Error inserting timerange");
}
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
return exportSession;
}
This does what it's supposed to on iOS8 but on iOS7 exportSession is always nil at the end of the method. I tried using assets with different formats (mp3, caf) but the result was the same.
If I play the composition instead of exporting it the sound is fine:
AVPlayerItem * item = [[AVPlayerItem alloc] initWithAsset:composition];
AVPlayer * player = [AVPlayer playerWithPlayerItem:item];
[player play];
Any idea about what might be wrong?
exportSession = [[AVAssetExportSession alloc]
initWithAsset:songAsset
presetName:AVAssetExportPresetAppleM4A];
exportSession.outputFileType = #"com.apple.m4a-audio";
filePath = [[docDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:#"m4a.mov"]
exportSession.outputURL = [NSURL fileURLWithPath:filePath];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSString *newPath = [NSString stringWithFormat:#"%#/%#", documentDirectory, [[[filePath lastPathComponent] substringToIndex:[[filePath lastPathComponent] length]-5] stringByAppendingString:#"a"]];
[[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:newPath error:&error];
}
if (exportSession.status == AVAssetExportSessionStatusCancelled) {
}
if (exportSession.status == AVAssetExportSessionStatusExporting) {
}
if (exportSession.status == AVAssetExportSessionStatusFailed) {
NSLog(#"erros:%#",exportSession.error);
}
if (exportSession.status == AVAssetExportSessionStatusUnknown) {
}
if (exportSession.status == AVAssetExportSessionStatusWaiting) {
}
}];
If you want to save in mp3 format, use ffmpeg.

Objective - C, how do I crop .caf file?

I've recorded sound into .caf file using AVAudioRecorder.
I want to cut/trim first 2 seconds of this file.
How to do that? (I've spent hours to find the solution, but no luck)
You can do this with AVAssetExportSession..
Try this code..
float vocalStartMarker = 0.0f;
float vocalEndMarker = 2.0f;
NSURL *audioFileInput = #"input filePath"; // give your audio file path
NSString *docsDirs;
NSArray *dirPath;
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDirs = [dirPath objectAtIndex:0];
NSString *destinationURLs = [docsDirs stringByAppendingPathComponent:#"trim.caf"];
audioFileOutput = [NSURL fileURLWithPath:destinationURLs];
if (!audioFileInput || !audioFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVURLAsset URLAssetWithURL:audioFileInput options:nil];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetAppleM4A];
if (exportSession == nil)
{
return NO;
}
CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
// It worked!
NSLog(#"DONE TRIMMING.....");
NSLog(#"ouput audio trim file %#",audioFileOutput);
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
NSLog(#"FAILED TRIMMING.....");
}
}];

[exportSession exportAsynchronouslyWithCompletionHandler: ^(void) {} giving export error?

I have made a sample app to trim a video by getting it from camera roll. Written code is as follows:
-(IBAction)cutVideo
{
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path=[path stringByAppendingPathComponent:#"new.mov"];
[self splitVideo:path];
}
- (void)splitVideo:(NSString *)outputURL
{
#try
{
NSURL *fileURL=[[NSURL alloc] init];
fileURL=[NSURL fileURLWithPath:outputURL];
fileURL=[NSURL URLWithString:outputURL];
// NSString *videoBundleURL = [[NSBundle mainBundle] pathForResource:#"samp" ofType:#"mov"];
AVAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
[self trimVideo:outputURL assetObject:asset];
}
// videoBundleURL = nil;
asset = nil;
compatiblePresets = nil;
}
#catch (NSException * e)
{
NSLog(#"Exception Name:%# Reason:%#",[e name],[e reason]);
}
}
- (void)trimVideo:(NSString *)outputURL assetObject:(AVAsset *)asset
{
#try
{
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime start = CMTimeMakeWithSeconds(startTime, 1);
CMTime duration = CMTimeMakeWithSeconds((endTime - startTime), 1);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
if ([[NSFileManager defaultManager] fileExistsAtPath:outputURL])
{
[[NSFileManager defaultManager] removeItemAtPath:outputURL error:nil];
}
[exportSession exportAsynchronouslyWithCompletionHandler: ^(void) {
NSLog(#"Export Status %d %#", exportSession.status, [exportSession.error description]);
}];
exportSession = nil;
}
#catch (NSException * e)
{
NSLog(#"Exception Name:%# Reason:%#",[e name],[e reason]);
}
}
all is working fine but error is in export file .. error is as follows
2012-12-12 13:27:27.896 RecordVideo[1472:907] Export Status 4 Error
Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=0x1e577ad0
{NSErrorFailingURLStringKey=/var/mobile/Applications/A38CC8B9-A8CB-4A65-8308-
24A9BEB27626/Library/Documentation/new.mov,
NSErrorFailingURLKey=/var/mobile/Applications/A38CC8B9-A8CB-4A65-8308-
24A9BEB27626/Library/Documentation/new.mov, NSLocalizedDescription=unknown error,
NSUnderlyingError=0x1e537da0 "The operation couldn’t be completed. (OSStatus error
-12935.)", NSURL=/var/mobile/Applications/A38CC8B9-A8CB-4A65-8308-
24A9BEB27626/Library/Documentation/new.mov}
Any help will be appreciated, thanks

Trim video without displaying UIVideoEditorController?

Currently I'm working on a application which deals with the videos.
In my application user can trim the video, I have a custom control for selecting the start time and end time. I need to trim the video by these two values. I tried with UIVideoEditorController like follows.
UIVideoEditorController* videoEditor = [[[UIVideoEditorController alloc] init] autorelease];
videoEditor.delegate = self;
NSString* videoPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"MOV"];
if ( [UIVideoEditorController canEditVideoAtPath:videoPath] )
{
videoEditor.videoPath = videoPath;
[self presentModalViewController:videoEditor animated:YES];
}
else
{
NSLog( #"can't edit video at %#", videoPath );
}
But the issue is the above code will display apple's video editor control and user can do some operations on that view. I don't want to display this view, because I have already displayed the video on MPMoviePlayer and received the user input (start time and end time) for trimming the video on a custom control.
How can I trim a video without displaying UIVideoEditorController ?
Finally I found the solution.
We can use AVAssetExportSession for trimming video without displaying UIVideoEditorController.
My code is like:
- (void)splitVideo:(NSString *)outputURL
{
#try
{
NSString *videoBundleURL = [[NSBundle mainBundle] pathForResource:#"Video_Album" ofType:#"mp4"];
AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoBundleURL] options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
[self trimVideo:outputURL assetObject:asset];
}
videoBundleURL = nil;
[asset release];
asset = nil;
compatiblePresets = nil;
}
#catch (NSException * e)
{
NSLog(#"Exception Name:%# Reason:%#",[e name],[e reason]);
}
}
This method trims the video
- (void)trimVideo:(NSString *)outputURL assetObject:(AVAsset *)asset
{
#try
{
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime start = CMTimeMakeWithSeconds(splitedDetails.startTime, 1);
CMTime duration = CMTimeMakeWithSeconds((splitedDetails.stopTime - splitedDetails.startTime), 1);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[self checkExportSessionStatus:exportSession];
[exportSession release];
exportSession = nil;
}
#catch (NSException * e)
{
NSLog(#"Exception Name:%# Reason:%#",[e name],[e reason]);
}
}
This method checks the status of trimming:
- (void)checkExportSessionStatus:(AVAssetExportSession *)exportSession
{
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch ([exportSession status])
{
case AVAssetExportSessionStatusCompleted:
NSLog(#"Export Completed");
break;
case AVAssetExportSessionStatusFailed:
NSLog(#"Error in exporting");
break;
default:
break;
}
}];
}
I'm calling the splitVideo method from the export button action method and passes the output URL as argument.
We can import AVFoundation/AVFoundation.h
-(BOOL)trimVideofile
{
float videoStartTime;//define start time of video
float videoEndTime;//define end time of video
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd_HH-mm-ss"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:#"Caches"];
NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:#"/output_%#.mov", [dateFormatter stringFromDate:[NSDate date]]];
NSURL *videoFileOutput = [NSURL fileURLWithPath:OutputFilePath];
NSURL *videoFileInput;//<Path of orignal Video file>
if (!videoFileInput || !videoFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:videoFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:videoFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetLowQuality];
if (exportSession == nil)
{
return NO;
}
CMTime startTime = CMTimeMake((int)(floor(videoStartTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(videoEndTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = videoFileOutput;
exportSession.timeRange = exportTimeRange;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"Export OK");
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
NSLog(#"Export failed: %#", [[exportSession error] localizedDescription]);
}
}];
return YES;
}

iOS Audio Trimming

I searched a lot and couldn't find anything relevant... I am working on iOS audio files and here is what I want to do...
Record Audio and Save Clip (Checked, I did this using AVAudioRecorder)
Change the pitch (Checked, Did this using Dirac)
Trimming :(
I have two markers i.e. starting & ending offset and using this info I want to trim recorded file and want to save it back. I don't want to use "seek" because later on I want to play all recorded files in sync (just like flash movie clips in timeline) and then finally I want to export as one audio file.
Here's the code that I've used to trim audio from a pre-existing file. You'll need to change the M4A related constants if you've saved or are saving to another format.
- (BOOL)trimAudio
{
float vocalStartMarker = <starting time>;
float vocalEndMarker = <ending time>;
NSURL *audioFileInput = <your pre-existing file>;
NSURL *audioFileOutput = <the file you want to create>;
if (!audioFileInput || !audioFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:audioFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetAppleM4A];
if (exportSession == nil)
{
return NO;
}
CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
// It worked!
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
}
}];
return YES;
}
There's also Technical Q&A 1730, which gives a slightly more detailed approach.
import following two libraries in .m
#import "BJRangeSliderWithProgress.h"
#import < AVFoundation/AVFoundation.h >
and after that paste following code you will be able to trim an audio file with the help of two thumbs.
- (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
mySlider = [[BJRangeSliderWithProgress alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];
[mySlider setDisplayMode:BJRSWPAudioSetTrimMode];
[mySlider addTarget:self action:#selector(valueChanged) forControlEvents:UIControlEventValueChanged];
[mySlider setMinValue:0.0];
NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"saewill.mp3"];
NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];
audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audioFileInput error:nil];
[mySlider setMaxValue:audioPlayer.duration];
[self.view addSubview:mySlider];
}
-(void)valueChanged {
NSLog(#"%f %f", mySlider.leftValue, mySlider.rightValue);
}
-(IBAction)playTheSong
{
// Path of your source audio file
NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"saewill.mp3"];
NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];
// Path of your destination save audio file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
//libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:#"Caches"];
NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:#"output.mov"];
NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:#"output.m4a"];
NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath];
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:audioFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetAppleM4A];
float startTrimTime = mySlider.leftValue;
float endTrimTime = mySlider.rightValue;
CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"Success!");
NSLog(#" OUtput path is \n %#", requiredOutputPath);
NSFileManager * fm = [[NSFileManager alloc] init];
[fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
//[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
NSError *error;
audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops=0;
[audioPlayer play];
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
NSLog(#"failed with error: %#", exportSession.error.localizedDescription);
}
}];
}
// Swift 4.2
If anybody is still looking for answer in swift here it is.
//Audio Trimming
func trimAudio(asset: AVAsset, startTime: Double, stopTime: Double, finished:#escaping (URL) -> ())
{
let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith:asset)
if compatiblePresets.contains(AVAssetExportPresetMediumQuality) {
guard let exportSession = AVAssetExportSession(asset: asset,
presetName: AVAssetExportPresetAppleM4A) else{return}
// Creating new output File url and removing it if already exists.
let furl = createUrlInAppDD("trimmedAudio.m4a") //Custom Function
removeFileIfExists(fileURL: furl) //Custom Function
exportSession.outputURL = furl
exportSession.outputFileType = AVFileType.m4a
let start: CMTime = CMTimeMakeWithSeconds(startTime, preferredTimescale: asset.duration.timescale)
let stop: CMTime = CMTimeMakeWithSeconds(stopTime, preferredTimescale: asset.duration.timescale)
let range: CMTimeRange = CMTimeRangeFromTimeToTime(start: start, end: stop)
exportSession.timeRange = range
exportSession.exportAsynchronously(completionHandler: {
switch exportSession.status {
case .failed:
print("Export failed: \(exportSession.error!.localizedDescription)")
case .cancelled:
print("Export canceled")
default:
print("Successfully trimmed audio")
DispatchQueue.main.async(execute: {
finished(furl)
})
}
})
}
}
You can use it for video trimming as well. For Video trimming replace the value of export session as bellow:
guard let exportSession = AVAssetExportSession(asset: asset,
presetName: AVAssetExportPresetPassthrough) else{return}
and filetype to mp4
exportSession.outputFileType = AVFileType.mp4
Here is a sample code which trim audio file from starting & ending offset and save it back.
Please check this iOS Audio Trimming.
// Path of your source audio file
NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"abc.mp3"];
NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];
// Path of your destination save audio file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:#"Caches"];
NSString *strOutputFilePath = [NSString stringWithFormat:#"%#%#",libraryCachesDirectory,#"/abc.mp4"];
NSURL *audioFileOutput = [NSURL fileURLWithPath:strOutputFilePath];
if (!audioFileInput || !audioFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:audioFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
if (exportSession == nil)
{
return NO;
}
float startTrimTime = 0;
float endTrimTime = 5;
CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(#"Success!");
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
NSLog(#"failed");
}
}];

Resources