Xcode:如何将MP4文件转换为音频文件 [英] Xcode: How to convert MP4-File to Audio-File

查看:156
本文介绍了Xcode:如何将MP4文件转换为音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将MP4文件从apps文件夹转换为音频文件(mp3或m4a)。
我已经试过,但是我不能用AVPlayer播放转换后的MP3文件。



这是我的代码:

   - (void)convertMP4toMP3withFile:(NSString *)dstPath 
{
NSURL * dstURL = [NSURL fileURLWithPath:dstPath];

AVMutableComposition * newAudioAsset = [AVMutableComposition构成];

AVMutableCompositionTrack * dstCompositionTrack;
dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

AVAsset * srcAsset = [AVURLAsset URLAssetWithURL:dstURL options:nil];
AVAssetTrack * srcTrack = [[srcAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];


CMTimeRange timeRange = srcTrack.timeRange;

NSError *错误;

if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcTrack atTime:kCMTimeZero error& error]){
NSLog(@track insert failed:%@ \\\
,错误);
return;


$ b AVAssetExportSession * exportSesh = [[AVAssetExportSession alloc] initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];

exportSesh.outputFileType = AVFileTypeAppleM4A;
exportSesh.outputURL = dstURL;

[[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];

[exportSesh exportAsynchronouslyWithCompletionHandler:^ {
AVAssetExportSessionStatus status = exportSesh.status;
NSLog(@exportAsynchronouslyWithCompletionHandler:%i \\\
,status);

if(AVAssetExportSessionStatusFailed == status){
NSLog(@FAILURE:%@ \\\
,exportSesh.error);
} else if(AVAssetExportSessionStatusCompleted == status){
NSLog(@SUCCESS!\\\
);


NSError *错误;
//以

的格式附加文件的名称//检查文件是否存在(完全不必要)。
NSString * onlyPath = [dstPath stringByDeletingLastPathComponent];
NSString * toPathString = [NSString stringWithFormat:@%@ / testfile.m4a,onlyPath];
[[NSFileManager defaultManager] moveItemAtPath:dstPath toPath:toPathString error:& error];
[self loadFiles];
}
}];





$ b

有没有人可以解决我的问题,或者可以改善我的代码?

解决方案

替换此行:

  exportSesh.outputFileType = AVFileTypeAppleM4A; 



  exportSesh.outputFileType = AVFileTypeCoreAudioFormat; 

&这:

  NSString * toPathString = [NSString stringWithFormat:@%@ / testfile.m4a,onlyPath]; 



  NSString * toPathString = [NSString stringWithFormat:@%@ / testfile.mp3,onlyPath]; 

为我工作:)

I want to convert a MP4-file from the apps Document folder into an Audio-file (mp3 or m4a). I already tried, but i can not play the converted MP3-file with AVPlayer.

Here is my code:

-(void)convertMP4toMP3withFile:(NSString*)dstPath
{
    NSURL *dstURL = [NSURL fileURLWithPath:dstPath];

AVMutableComposition*   newAudioAsset = [AVMutableComposition composition];

AVMutableCompositionTrack*  dstCompositionTrack;
dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

AVAsset*    srcAsset = [AVURLAsset URLAssetWithURL:dstURL options:nil];
AVAssetTrack*   srcTrack = [[srcAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];


CMTimeRange timeRange = srcTrack.timeRange;

NSError*    error;

if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcTrack atTime:kCMTimeZero error:&error]) {
    NSLog(@"track insert failed: %@\n", error);
    return;
}


AVAssetExportSession*   exportSesh = [[AVAssetExportSession alloc] initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];

exportSesh.outputFileType = AVFileTypeAppleM4A;
exportSesh.outputURL = dstURL;

[[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];

[exportSesh exportAsynchronouslyWithCompletionHandler:^{
    AVAssetExportSessionStatus  status = exportSesh.status;
    NSLog(@"exportAsynchronouslyWithCompletionHandler: %i\n", status);

    if(AVAssetExportSessionStatusFailed == status) {
        NSLog(@"FAILURE: %@\n", exportSesh.error);
    } else if(AVAssetExportSessionStatusCompleted == status) {
        NSLog(@"SUCCESS!\n");


        NSError *error;
        //append the name of the file in jpg form

        //check if the file exists (completely unnecessary).
        NSString *onlyPath = [dstPath stringByDeletingLastPathComponent];
        NSString *toPathString = [NSString stringWithFormat:@"%@/testfile.m4a", onlyPath];
        [[NSFileManager defaultManager] moveItemAtPath:dstPath toPath:toPathString error:&error];
        [self loadFiles];
    }
}];
}

Has anyone a solution for my problem or can improve my code?

解决方案

replace this line:

exportSesh.outputFileType = AVFileTypeAppleM4A;

with:

exportSesh.outputFileType = AVFileTypeCoreAudioFormat;

& this:

NSString *toPathString = [NSString stringWithFormat:@"%@/testfile.m4a", onlyPath];

with:

NSString *toPathString = [NSString stringWithFormat:@"%@/testfile.mp3", onlyPath];

Worked for me :)

这篇关于Xcode:如何将MP4文件转换为音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆