swiftavfoundationcore-audioaacaiff

Exporting AIFF to AAC using Apple's AVFoundationExporter sample code gets me something slightly broken


I've downloaded Apple's sample code AVFoundationExporter (available from the Apple dev site). The title of the project is "AVFoundationExporter: Exporting and Transcoding Movies", but I'm trying to use it to change an AIFF file to an AAC (M4A) file.

On the desktop, I have a file "testfile.aiff". I invoke it with the command...

AVFoundationExporter "/Users/rbell/Desktop/testfile.aiff" "/Users/rbell/Desktop/testfile.m4a" -preset AVAssetExportPresetAppleM4A -f public.mpeg-4-audio"

(or just [AVFoundationExporter "/Users/rbell/Desktop/testfile.aiff" "/Users/rbell/Desktop/testfile.m4a"])

... I do indeed get a file called "testfile.m4a" on my desktop. And I can indeed play it using Finder's spacebar preview, or using QuickTime.

However, I cannot open it with or import it into iTunes. It simply won't go. I can open the .aiff file just fine, and it plays happily. But when I try to force the .m4a file to "Open in... iTunes", iTunes opens and nothing happens. The .m4a file does not appear in iTunes anywhere.

I tried changing the line

var destinationFileType = AVFileTypeQuickTimeMovie

to

var destinationFileType = AVFileTypeAppleM4A

And it now fails with

error: Operation Stopped: The operation is not supported for this media.. Program ended with exit code: 1

I've fiddled a lot of settings, and nothing seems to work. The -inject-metadata flag doesn't make any difference. Neither does using a -preset. -fileType com.apple.m4a-audio doesn't help. The strange thing is that I really do get the file I want. It just doesn't work properly. If I open my "testfile.m4a" file in QuickTime and export it again, then I end up with an .m4a file with a different bitrate, but this one does open in iTunes.


Solution

  • The file type public.mpeg-4-audio (from kUTTypeMPEG4Audio?) is wrong. You need com.apple.m4a-audio (which corresponds to AVFileTypeAppleM4A).

    Don't forget to remove the existing m4a file first!

    So your command should be

    rm -f out.m4a  # AVFoundation doesn't like overwriting existing files.
    AVFoundationExporter in.aiff out.m4a -preset AVAssetExportPresetAppleM4A -f public.mpeg-4-audio