Hello guest, if you like this forum, why don't you register? https://fanrestore.com/member.php?action=register (December 14, 2021) x


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script to make MKVs importable in Premiere
#1
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.

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.
Reply
Thanks given by: interaser , PDB , SIUse , alleycat
#2
(2021-08-18, 03:28 PM)TomArrow Wrote: 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.

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.

Thanks I've had this issue in the past and it was a pain so this will certainly come in handy.
Reply
Thanks given by:


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thumbnail script with scene detection deleted user 1 3,021 2020-01-04, 04:40 PM
Last Post: deleted user
  AviSynth Deblur (A Simple Sharpening Script) MWilson 6 8,987 2018-04-11, 02:51 PM
Last Post: MWilson
  AviSynth Rigby Reardon's Mondo Sync Script for TGtBatU Chewtobacca 0 2,776 2016-11-13, 01:27 AM
Last Post: Chewtobacca
  Script snippets subforum Feallan 3 6,372 2016-11-13, 12:32 AM
Last Post: Feallan

Forum Jump:


Users browsing this thread: 1 Guest(s)