nschlia / ffmpegfs

FUSE-based transcoding filesystem with video support from many formats to FLAC, MP4, TS, WebM, OGG, MP3, HLS, and others.
https://nschlia.github.io/ffmpegfs/
GNU General Public License v3.0
198 stars 14 forks source link

Please add ALAC support #37

Closed beren12 closed 4 years ago

beren12 commented 5 years ago

Apple Lossless is available via ffmpeg or other tools, it's pretty fast and great for iTunes users as it takes up less space than wav/aiff.

nschlia commented 5 years ago

Sounds interesting, I'll have to check if this works (i.e. if it is possible to generate the format on-the-fly).

beren12 commented 5 years ago

Awesome.

nschlia commented 4 years ago

ALAC support has been added (experimental). The Apple lossless files are created in a MOV container.

Works so far, but the result files are even larger than WAVs. Still have to figure that one out.

To enable, set "desttype=alac".

beren12 commented 4 years ago

Hmm it should be close to flac, and it should be in an mp4 container. XLD is an open source program that does it.

nschlia commented 4 years ago

Hmm it should be close to flac, and it should be in an mp4 container. XLD is an open source program that does it.

With FFmpeg this seems to be a bit tricky. Although others do use MP4 as container, FFmpeg supports MOV only (which obviously is what Apple intends). When I try to use MP4, I get this error:

[mp4 @ 0x55b3c5610680] Could not find tag for codec alac in stream #1, codec not currently supported in container
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument]

Happens with the orginal ffmpeg using this command line:

ffmpeg -i '/mnt/mp3base1/audio2/flac/B/Battle Beast/Battle Beast/12. Battle Beast - Black Ninja.flac' -c:a alac -f mp4 test.mp4

While this works:

ffmpeg -i '/mnt/mp3base1/audio2/flac/B/Battle Beast/Battle Beast/12. Battle Beast - Black Ninja.flac' -c:a alac -f mov test.mov

So I guess there's no way around MOV. But this should be no problem, every software that supports ALAC should also support MOV as it is the designated container format. It's also well tested with ffmpegfs and so ALAC should work.

For the size problem I still need to find a solution. Using the command above with ffmpeg I get these results:

Creator Format Size %
wav 42725566 100,00 %
ffmpeg flac 32076475 75,08 %
ffmpeg alac 32672094 76,47 %
ffmpegfs alac 60654820 141,96 %

The resulting file is even larger than the original uncompressed WAV. That's not the idea behind ALAC, i suppose :) I guess I'll have to set some parameters, I need to find out which.

nschlia commented 4 years ago

OK, fixed that one: Seems that if the source sample format is packed (and ALAC always expects planar) it could happen that the format is resampled from 16 to 32 bits, resulting in a much larger file.

I changed the logic that the bit depth stays 8, 16, 24 or 32 bits and only packed to planar conversion is performed.

Now the file's size is down to ~65 % of the WAV size and just 1 % larger than the FLAC. This is the expected result :)

beren12 commented 4 years ago

ALAC is not common in mov actually. Wikipedia: "Apple Lossless data is frequently stored within an MP4 container with the filename extension .m4a. This extension is also used by Apple for lossy AAC audio data in an MP4 container (same container, different audio encoding)." "While not nearly as common, the ALAC format can also use the .CAF file type container."

Itunes also puts it in a m4a file.

nschlia commented 4 years ago

I am not sure I can put it in MP4 with FFmpeg without a patch to FFmpeg. I'll see what I can do, but probably you either need to use a patched FFmeg (build it yourself, not for the faint hearted if you ask me. FFmpeg is not so easy to build with all required libs) or I can find a way to get around the restricting.

I just tried to create a ALAC MP with a FFmpeg version built from git (N-94168-g0f39ef4db2 pulled two days ago) and it still refuses to put ALAC in MP4 containers.

Is it really such a big issue to use MOV instead?

nschlia commented 4 years ago

This is actually a FFmpeg issue.

To fix it I either need to file a bug report, hoping someone will fix it. Or make a patch (which is maybe easy for this one, but could also be nasty) and send it in, also hoping someone will apply it upstream.

I'll try to do that but I can't promise it will help.

beren12 commented 4 years ago

ffmpeg -i track.flac -acodec alac track.m4a works.

nschlia commented 4 years ago

"-acodec alac" is the deprecated version of "-c:a alac" which is still accepted by ffmpeg. But you are right, m4a is accepted wihle mp4 is not. Although both create the same container...

nschlia commented 4 years ago

I finally found out how to create ALAC files inside a MP4 container with the m4a extension. It seems it makes a difference for the FFmpeg API if m4a is used instead of mp4. Although the format should be the same container, it creates a different flavour that accepts the ALAC codec.

Hope this is now the correct solution. Thanks for insisting on it :)

beren12 commented 4 years ago

Awesome thanks for that!

nschlia commented 4 years ago

Awesome thanks for that!

You're welcome.

nschlia commented 4 years ago

Added test. Now really to be closed.