2021-08-18, 03:28 PM
Okay, it's a bit of a clickbait. The script actually just converts mkvs to mp4. However, it solves a problem that has always personally plagued me: Audio. How do you manage to end up with audio inside Premiere that doesn't require lossy compression? Convert to wav and import separately? Good luck with 24 bit 5.1 audio that will break the 4 GB limit on WAV files. Use W64 instead? Premiere won't import it. So, split the audio to individual sub-4GB wav files.
It's annoying as heck...
What if we could just have the normal audio losslessly inside the mp4 to begin with?
That's what this script does.
Save this in a .bat file and you need to have ffmpeg and mp4box in the PATH.
It will find every .mkv file in the current folder, see if an identically named file with the .mp4 extension exists, and if it doesn't, it will get to work creating it. It will first convert the audio to a 32 bit floating point .caf file. Then it will use mp4box to package that along with the original video into an .mp4.
The resulting file will be (sometimes much) bigger than the original mkv because the audio is now 32 bit lossless. But: It works. And it's lossless.
Why CAF? Why 32 bit float? Why not directly write the PCM using ffmpeg? Why mp4 and not just .m2ts or something? Well, I tried a couple combinations and this is the only one I found that both works with mp4box, that doesn't lose quality on 24 bit audio, that works for resulting audio sizes over 4 GB and that is recognized and actually works in Premiere. Iirc 16 bit worked too, but 24 bit did not. Therefore 32 bit it is. CAF because it's the >4GB version of AIFF, and AIFF works whereas WAV doesn't because mp4 requires big-endian audio apparently and WAV doesn't support that and if you do feed little-endian WAV into mp4box, it will mux it, but it will still think it's big-endian and you'll just get static. And you can't write PCM directly to mp4 using ffmpeg because ffmpeg refuses to do that because it's not *officially* supported. And mp4 as the choice for the final output because Premiere seems to most reliably seek frames in mp4 files, whereas it will open .ts/.m2ts containers but often end up showing artifacts or the wrong frame.
Hope this will come in handy for someone.
It's annoying as heck...
What if we could just have the normal audio losslessly inside the mp4 to begin with?
That's what this script does.
Code:
for %%f in (*.mkv) do (
if exist "%%~nf.mp4" (
echo %%~nf
echo "already exists"
) else (
rem ffmpeg -i "%%~nf.mkv" -vcodec copy -acodec alc "%%~nf.mp4"
ffmpeg -drc_scale 0.000000 -i "%%~nf.mkv" -vn -acodec pcm_f32be "%%~nf.af4354b456nbtysdge3r5.caf"
mp4box -add "%%~nf.mkv#video" -add "%%~nf.af4354b456nbtysdge3r5.caf#audio" -new "%%~nf.mp4"
del "%%~nf.af4354b456nbtysdge3r5.caf"
)
rem process_in "%%~nf.mkv"
rem process_out "%%~nf.out"
)
pause
Save this in a .bat file and you need to have ffmpeg and mp4box in the PATH.
It will find every .mkv file in the current folder, see if an identically named file with the .mp4 extension exists, and if it doesn't, it will get to work creating it. It will first convert the audio to a 32 bit floating point .caf file. Then it will use mp4box to package that along with the original video into an .mp4.
The resulting file will be (sometimes much) bigger than the original mkv because the audio is now 32 bit lossless. But: It works. And it's lossless.
Why CAF? Why 32 bit float? Why not directly write the PCM using ffmpeg? Why mp4 and not just .m2ts or something? Well, I tried a couple combinations and this is the only one I found that both works with mp4box, that doesn't lose quality on 24 bit audio, that works for resulting audio sizes over 4 GB and that is recognized and actually works in Premiere. Iirc 16 bit worked too, but 24 bit did not. Therefore 32 bit it is. CAF because it's the >4GB version of AIFF, and AIFF works whereas WAV doesn't because mp4 requires big-endian audio apparently and WAV doesn't support that and if you do feed little-endian WAV into mp4box, it will mux it, but it will still think it's big-endian and you'll just get static. And you can't write PCM directly to mp4 using ffmpeg because ffmpeg refuses to do that because it's not *officially* supported. And mp4 as the choice for the final output because Premiere seems to most reliably seek frames in mp4 files, whereas it will open .ts/.m2ts containers but often end up showing artifacts or the wrong frame.
Hope this will come in handy for someone.