How can I extrace Audio from Video file without using FFmpeg?
I want to use AVMutableComposition
and AVURLAsset
for solving it.e.g. conversion from .mov to .m4a file.
How can I extrace Audio from Video file without using FFmpeg?
I want to use AVMutableComposition
and AVURLAsset
for solving it.e.g. conversion from .mov to .m4a file.
In all multimedia formats, audio is encoded separately from video, and their frames are interleaved in the file. So removing the video from a multimedia file does not require any messing with encoders and decoders: you can write a file format parser that will drop the video track, without using the multimedia APIs on the phone.
To do this without using a 3rd party library, you need to write the parser from scratch, which could be simple or difficult depending on the file format you wish to use. For example, FLV is very simple so stripping a track out of it is very easy (just go over the stream, detect the frame beginnings and drop the '0x09'=video frames). MP4 a bit more complex, its header (MOOV) has a hierarchical structure in which you have headers for each of the tracks (TRAK atoms). You need to drop the video TRAK, and then copy the interleaved bitstream atom (MDAT) skipping all the video data clusters as you copy.
There are 3rd party libraries you can use, aside from ffmpeg. One that comes in mind is GPAC MP4BOX (LGPL license). If the LGPL is a problem, there are plenty of commercial SDKs that you can use.
The following Swift 3 code shows how to extract audio from a movie file (.mov) and convert it to an audio file (.m4a) by using
AVURLAsset
,AVMutableComposition
andAVAssetExportSession
: