OGeek|极客世界-中国程序员成长平台

标题: objective-c - objective-c - 将 mpmediaitem 转换为 ogg vorbis [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:31
标题: objective-c - objective-c - 将 mpmediaitem 转换为 ogg vorbis

我找到(自定义我找到的方法)一种转换 MpMediaItem 并获取 mp3 文件的方法。可能这不是最好的方法,但它确实有效。

有没有办法获得 *.ogg 文件而不是 *.mp3 ?什么是正确的方法?

这是我用来获取 *.mp3 文件的方法:

    -(void)exportMP3NSURL*)url toFileUrlNSString*)fileURL
{
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetReader *reader=[[AVAssetReader alloc] initWithAsset:asset error:nil];
    NSMutableArray *myOutputs =[[NSMutableArray alloc] init];
    for(id track in [asset tracks])
    {
        AVAssetReaderTrackOutput *output=[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil];
        [myOutputs addObjectutput];   
        [reader addOutpututput];
    }
    [reader startReading];
    NSFileHandle *fileHandle ;
    NSFileManager *fm=[NSFileManager defaultManager];
    if(![fm fileExistsAtPath:fileURL])
    {
        [fm createFileAtPath:fileURL contents:[[NSData alloc] init] attributes:nil];
    }else{
        NSURL *url = [NSURL URLWithString:fileURL];
        [fm removeItemAtURL:url error:nil];
        [fm createFileAtPath:fileURL contents:[[NSData alloc] init] attributes:nil];

    }
    fileHandle=[NSFileHandle fileHandleForWritingAtPath:fileURL];    
    [fileHandle seekToEndOfFile];

    AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];
    int totalBuff=0;
    int test = 1;
    while(test == 1)
    {
        CMSampleBufferRef ref=[output copyNextSampleBuffer];

        if(ref==NULL)
            test = 0;

        if(ref==NULL)
            break;
        //copy data to file
        //read next one
        AudioBufferList audioBufferList;
        NSMutableData *data;
        CMBlockBufferRef blockBuffer;
        CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);

        for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
        {
            AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
            void *frame = audioBuffer.mData;

            data = [[NSMutableData alloc] initWithBytes:frame length:audioBuffer.mDataByteSize];
        }
        totalBuff++;
        NSLog(@"\n%d\n",totalBuff);

        int time = [self.mediaPlayer durationOfCurrentItem];

        if (totalBuff * 2 <= time + 1)        
            [fileHandle writeData:data];

    }
    [fileHandle closeFile];
}



Best Answer-推荐答案


您可以使用 libvorbis将 PCM 数据编码为 Vorbis。

关于objective-c - objective-c - 将 mpmediaitem 转换为 ogg vorbis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526754/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4