NSUrlConnection을 사용하여 YouTube 비디오를 다운로드하고 장치에 저장하도록 관리했습니다. 이제이 파일 (.mp4)을 .mp3 오디오 파일로 변환하고 싶습니다. 누구든지이 문제에 대한 해결책을 알고 있습니까? 비디오에서 오디오 만 다운로드 할 수있는 방법이 있습니까? 이것은 많은 시간을 절약 할 것입니다.
우선, 당신은 어떤 것도 변환하고 싶지 않습니다. 대신 mp4 파일에서 오디오 스트림을 추출하려고합니다. AVMutableComposition
원본 파일의 오디오 트랙 만 포함 된을 만든 다음 AVAssetExportSession
. 이것은 현재 m4a 중심입니다. m4a 및 mp3 출력을 모두 처리하려면 오디오 트랙 유형을 확인하고 올바른 파일 확장자를 설정했는지 확인 하고 내보내기 세션에서 AVFileTypeMPEGLayer3
또는 중에서 선택하십시오 AVFileTypeAppleM4A
.
NSURL* dstURL = [NSURL fileURLWithPath:dstPath];
[[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];
AVMutableComposition* newAudioAsset = [AVMutableComposition composition];
AVMutableCompositionTrack* dstCompositionTrack;
dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAsset* srcAsset = [AVURLAsset URLAssetWithURL:srcURL 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;
[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");
}
}];
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다