VideoHelp Forum




+ Reply to Thread
Results 1 to 11 of 11
  1. mkvoptimize Toolkit

    The mkvoptimize Toolkit is a set of video optimization scripts that aims at optimizing all video, audio and subtitle streams in a video file to save space with a minimum of quality loss in the video stream, to convert audio to stereo (optional), and to convert text subtitles to a stripped form of SSA that gives your video player full font control. The kit consists of 3 tools:



    mkvoptimize (BETA)

    mkvoptimize doesn't do much to the audio, that's mkaoptimize's job. All this will do to the audio is convert your audio to AAC without specifying a bitrate, unless it's already AAC in which case the stream will simply be copied. If you made a stereo track beforehand with mkaoptimize then you have the option of either replacing your surround sound track, or adding a stereo track too. Going stereo-only will DRASTICALLY reduce the size of the final output.

    Video and subtitles are where mkvoptimize truly shines. I spent years messing around with methods of video conversion, trying to find a good balance between video quality and filesize, but not just that, I was also interested in keeping the resources required to run the video file, mainly CPU and GPU, but also memory and disk, to a minimum. After a while I figured out a few things, like how more highly compressed formats require more processing power to extract, and how ffmpegs h264 2-pass system is superior to QP or CRF. After a while I built the perfectly engineered command I wanted, a command that can turn a 1080p movie into roughly 1.5-2 gigs whatever the original size was and without sacrificing much quality, while also making it playable on a stock dual-core with no video card! This command became the mkvoptimize core command. mkvoptimize works on other resolutions too, but as far as my 1080p tests go thats about the average. Now I mentioned how my 2-pass command is superior to CRF or QP, now I'll explain why. First we scan the file first (pass 1) and generate a stats file that tells ffmpeg details about the file such as bitrate and/or filesize, but not in a general manner, it's more specific to a given area in the timeline. In other words, it scans the entire video and tells ffmpeg what parts of the video are intensive (action-oriented scenes with epic special effects) and which parts are non-intensive (just people sitting, talking, not moving much, no special effects) and this lets us target those specific areas in a video that take the most CPU and GPU to process. To summarize, mkvoptimize doesn't just reduce filesize, it acts like a RESOURCE EQUALIZER, specifically by lowering quality more or less depending on how intensive each individual scene in a video is. So pass 1 scans, pass 2 converts, and 2-pass also gives us a lot more settings to configure than CRF or QP, which means a lot more control over the conversion process.

    Subtitles are something else mkvoptimize will alter, at least your text-based subtitles anyway. In order to give your video player full control over font size, colour, position, etc. and to convert your subtitles to a smaller, easier to edit format that doesn't rely on index numbers, mkvoptimize will first divide your text-based and image-based subtitles into 2 files, then it will convert to text-based SRT to remove formatting, then to SSA, and then, finally I edit every subtitle with sed, removing the topmost formatting information too. Now we have a stripped substation alpha (SSSA) file for every sub. The script then recombines everthing and does so in a manner that preserves default track, forced track, name and language flags. Preserving and then reassigning those flags dynamically was not easy.

    Font files are included in the final output, but the script doesn't alter them in any way.

    Please use and edit the tool as much as you like, everything in here is covered under the GNU Public License including the assets. Installation is simple, just extract the assets zip file and put everything in there in the system32 and SysWow64 directories. The zip file inclides:

    ffmpeg
    ffprobe
    mkvmerge
    mkvextract
    mkvinfo
    sed
    Plus 3 dll files that sed requires to run

    After that you can use either mkvoptimize.bat or mkvoptimize.exe, they are the same, just in different formats. Operation is simple, either double click the file to convert every video file in the current directory, or, drag and drop a SINGLE video file onto the mkvoptimize file to convert just one file. Either way the files you work on will need to be in the same folder as mkvoptimize, good thing all its assets are external. (It's only 500kbs!) Files will output to a subfolder named C which will be automatically created, many temporary files will also be generated in C during the conversion process but they will be deleted when the script ends.



    mkaoptimize (COMING SOON)

    As I said above, this script will aim to convert any type of audio track to stereo in order to save space.



    mksoptimize (COMING SOON)

    You can use this tool to convert and extract subs without having to convert the entire mkv file.



    You can get the mkvoptimize Toolkit here, use it as much as you like just please remember this is a BETA so if you see a bug please report it, kaythanks: https://github.com/imkira3/mkvoptimize/tree/main
    Last edited by imkira3; 27th Aug 2025 at 10:01. Reason: Grammar correction
    Quote Quote  
  2. Originally Posted by Baldrick View Post
    Hmmm that's odd, would you try re-compiling the exe with Bat to Exe Converter 3.2 and scanning the output? If you do you should see the same result, and yet, by examining the code of my batch file directly, you can indeed confirm there are no viruses. So why was this detected as a virus? It's quite simple, many virus protection software and scanners automatically assume a file is a virus if it doesn't have a certificate it recognizes. You really should take virus scanners with a grain of salt bro...

    In point of fact, many virus protection software is a lot more stupid than people tend to assume. There are scanners that work by comparing md5 hashes, but one small insignificant change to the virus file is enough to throw them off. Then there is the certificate method. As for scanning files directly, have you ever tried reverse engineering a program? It's not so easy as it sounds, not if you don't have the source files, and depending on language and encryption the virus software may not be able to scan the file AT ALL.

    So how DO you protect yourself from viruses? Well, heres what I do, I use a program called strun to keep a close eye on my startup entries, I check them each time I reboot, and I use an advanced task manager called Process Explorer to keep a CPU window open at all times in my taskbar. I watch for unusual patterns, high network, high CPU, strange svchost tasks, stuff like that. This won't be much help finding dll-based viruses, but it's my best method.

    The future of virus protection:

    If you ask me, the smart thing to do would be to gravitate away from all programming languages that can't be scanned easily, and then use AI-based virus scanners that are actually smart enough to interpret the intended function behind codes and scan reverse-engineered code for nefarious intent, instead of the very basic methods we have today. The problem with that is developers can't hide their code, so copying someones work would be wayyy easy and so many people would just start copying programs, suppressing innovation. It's a catch-22 and I'm not sure how to proceed, just do your best and keep a close eye on running tasks.

    NOTE: There are actually programs like Sandboxie that let you run a program in a "sandbox". That means the program runs in a fully isolated environment, with a fake C drive and fake registry hives, it's a great way to see what a program or installer actually does I have run actual malware in sandboxes without consequence, for example, I once tried to delete my C drive in a sandbox using a simple batch file, it deleted the fake C drive instead, see what I mean?
    Last edited by imkira3; 27th Aug 2025 at 07:39.
    Quote Quote  
  3. Does this actually work at all?

    Looking at https://github.com/imkira3/mkvoptimize/blob/main/mkvoptimize%20v1.0.B.bat
    I see that video is always encoded with:
    Code:
    :: Step 6: Extract and optimize the video, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="1" ( ffmpeg -i "%INPUT%" -filter:v fps=23.976 -threads 0 -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=1:stats=C/SLT" -f null NUL && ffmpeg -y -i "%INPUT%" -filter:v fps=23.976 -threads 0 -map 0:v:0 -map 0:t? -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=2:stats=C/SLT" C\VAFC.mkv )
    
    :: Step 7: Extract and optimize the audio, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="2" ( ffmpeg -y -i "%INPUT%" -sn -map 0:a? -c:a aac -map 0:t? C\VAFC.mkv )
    
    :: Step 8: Extract and optimize the video, audio, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="3" ( ffmpeg -i "%INPUT%" -filter:v fps=23.976 -threads 0 -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=1:stats=C/SLT" -f null NUL && ffmpeg -y -i "%INPUT%" -filter:v fps=23.976 -threads 0 -map 0:v:0 -map 0:a? -map 0:t? -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=2:stats=C/SLT" -c:a aac C\VAFC.mkv )
    won't that cause issues if:
    a. video isn't 23.976 fps
    b. a bit rate of 1200kBit/s is really just too low

    Also looking at, for example:
    Code:
    :: Step 31: Detect the imagebased subtitle track IDs from the third output and store the data in a file
    if exist C\S.mks ( ffprobe -v error -select_streams s -show_entries stream=index,codec_name -of csv=p=0 C\S.mks | findstr /R /C:"bd_subtitle" /C:"dvb_subtitle" /C:"dvb_teletext" /C:"dvd_subtitle" /C:"dvdsub" /C:"hdmv_pgs" /C:"hdmv_pgs_subtitle" /C:"pgs" /C:"pgs_subtitle" /C:"vobsub" /C:"xsub" > C\ISC && sed -n "s/^\([0-9]\+\),.*$/\1/p" C\ISC > C\IS && del C\ISC )
    
    :: Step 32: Detect the textbased subtitle track IDs from the third output and store the data in a file
    if exist C\S.mks ( ffprobe -v error -select_streams s -show_entries stream=index,codec_name -of csv=p=0 C\S.mks | findstr /R /C:"arib_caption" /C:"ass" /C:"eia_608" /C:"hdmv_text_subtitle" /C:"jacosub" /C:"microdvd" /C:"mov_text" /C:"mpl2" /C:"pjs" /C:"realtext" /C:"sami" /C:"srt" /C:"ssa" /C:"stl" /C:"subrip" /C:"subviewer" /C:"subviewer1" /C:"text" /C:"ttml" /C:"vplayer" /C:"vtt" /C:"webvtt" > C\TSC && sed -n "s/^\([0-9]\+\),.*$/\1/p" C\TSC > C\TS && del C\TSC )
    and comparing it to https://learn.microsoft.com/de-de/windows-server/administration/windows-commands/
    Where does 'sed' come from? (I know it from linux)

    I might be wrong, but this whole thing, at least atm., looks to me like some llma generated not working code/project,...

    Cu Selur
    users currently on my ignore list: deadrats, Stears555, marcorocchini
    Quote Quote  
  4. Originally Posted by Selur View Post
    Does this actually work at all?

    Looking at https://github.com/imkira3/mkvoptimize/blob/main/mkvoptimize%20v1.0.B.bat
    I see that video is always encoded with:
    Code:
    :: Step 6: Extract and optimize the video, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="1" ( ffmpeg -i "%INPUT%" -filter:v fps=23.976 -threads 0 -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=1:stats=C/SLT" -f null NUL && ffmpeg -y -i "%INPUT%" -filter:v fps=23.976 -threads 0 -map 0:v:0 -map 0:t? -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=2:stats=C/SLT" C\VAFC.mkv )
    
    :: Step 7: Extract and optimize the audio, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="2" ( ffmpeg -y -i "%INPUT%" -sn -map 0:a? -c:a aac -map 0:t? C\VAFC.mkv )
    
    :: Step 8: Extract and optimize the video, audio, font and chapter tracks
    del C\V >nul 2>&1 && del C\A >nul 2>&1 && del C\VA >nul 2>&1 && if "%MODE%"=="3" ( ffmpeg -i "%INPUT%" -filter:v fps=23.976 -threads 0 -an -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=1:stats=C/SLT" -f null NUL && ffmpeg -y -i "%INPUT%" -filter:v fps=23.976 -threads 0 -map 0:v:0 -map 0:a? -map 0:t? -sn -c:v libx264 -preset veryfast -tune film -b:v 1200k -maxrate 1500k -bufsize 2000k -x264-params "pass=2:stats=C/SLT" -c:a aac C\VAFC.mkv )
    won't that cause issues if:
    a. video isn't 23.976 fps
    b. a bit rate of 1200kBit/s is really just too low

    Also looking at, for example:
    Code:
    :: Step 31: Detect the imagebased subtitle track IDs from the third output and store the data in a file
    if exist C\S.mks ( ffprobe -v error -select_streams s -show_entries stream=index,codec_name -of csv=p=0 C\S.mks | findstr /R /C:"bd_subtitle" /C:"dvb_subtitle" /C:"dvb_teletext" /C:"dvd_subtitle" /C:"dvdsub" /C:"hdmv_pgs" /C:"hdmv_pgs_subtitle" /C:"pgs" /C:"pgs_subtitle" /C:"vobsub" /C:"xsub" > C\ISC && sed -n "s/^\([0-9]\+\),.*$/\1/p" C\ISC > C\IS && del C\ISC )
    
    :: Step 32: Detect the textbased subtitle track IDs from the third output and store the data in a file
    if exist C\S.mks ( ffprobe -v error -select_streams s -show_entries stream=index,codec_name -of csv=p=0 C\S.mks | findstr /R /C:"arib_caption" /C:"ass" /C:"eia_608" /C:"hdmv_text_subtitle" /C:"jacosub" /C:"microdvd" /C:"mov_text" /C:"mpl2" /C:"pjs" /C:"realtext" /C:"sami" /C:"srt" /C:"ssa" /C:"stl" /C:"subrip" /C:"subviewer" /C:"subviewer1" /C:"text" /C:"ttml" /C:"vplayer" /C:"vtt" /C:"webvtt" > C\TSC && sed -n "s/^\([0-9]\+\),.*$/\1/p" C\TSC > C\TS && del C\TSC )
    and comparing it to https://learn.microsoft.com/de-de/windows-server/administration/windows-commands/
    Where does 'sed' come from? (I know it from linux)

    I might be wrong, but this whole thing, at least atm., looks to me like some llma generated not working code/project,...

    Cu Selur
    Why would I release a beta that doesn't work? Besides it's a batch file so if you don't like the settings, change them. But you would be surprised how well this works, the methods are a bit different thanyour thinking, were setting a min and max bitrate not a constant rate (like CRF does). It work more like QP, except QP doesn't set a maximum. This part here: "-b:v 1200k -maxrate 1500k" see the maxrate part? That's how high it can go, the other part is how low it can go. 1200 is fine for a scene where people are just talking, and setting a max of 1500 reduces quality mostly in those extra-intensive scenes. If you look at the history folder, you can see just how much work I put into it, it wasn't generated by anything lol, made it myself :P and don't knock it til you try it bro, like I said I have been adjusting the core command for years, finding the perfect settings.

    As for the fps, it's perfectly fine for most things but I guess if you were converting something with a super high FPS like sports at 60 or 120fps you would notice the difference, but this was built for movies and anime so I think it's fine, adjust it if needed.

    And sed is what your thinking, same as the linux version, just for windows instead. I use it to alter the subs. It's part of the gnuwin32 project, it's a Minux port.

    You know, it's pretty rude to just assume it won't work, did you even try it first?
    Last edited by imkira3; 27th Aug 2025 at 22:26.
    Quote Quote  
  5. It's part of the gnuwin32 project, it's a Minux port.
    You might want to mention that this is a requirement in your README.md, otherwise the batch script will not work.

    Also, 1200kBit/s might be fine for SD anime, but otherwise it's too low for my taste, especially when dealing with HD or UHD content.
    => you might want to overwork your README.md and state dependencies and that users might need to edit the scripts,...

    As for the fps, it's perfectly fine for most things but I guess if you were converting something with a super high FPS like sports at 60 or 120fps you would notice the difference, but this was built for movies and anime so I think it's fine, adjust it if needed.
    If your content is for example PAL (25 or 50fps) converting the video to NTSCs 23.976 fps will cause async.

    Cu Selur
    users currently on my ignore list: deadrats, Stears555, marcorocchini
    Quote Quote  
  6. Originally Posted by Selur View Post
    It's part of the gnuwin32 project, it's a Minux port.
    You might want to mention that this is a requirement in your README.md, otherwise the batch script will not work.

    Also, 1200kBit/s might be fine for SD anime, but otherwise it's too low for my taste, especially when dealing with HD or UHD content.
    => you might want to overwork your README.md and state dependencies and that users might need to edit the scripts,...

    If your content is for example PAL (25 or 50fps) converting the video to NTSCs 23.976 fps will cause async.

    Cu Selur
    I did mention it actually, I put sed in the Assets.zip file and explained how to install everything system-wide. Or did you mean I should link to it? I guess I did forget to do that on github. As for the sync and quality issues, please keep in mind that this is just a BETA. It's going to have a lot more features later on, like the ability to set a min/max bitrate, and the ability to override the default fps settings. Right now my main concern is testing the code in alternative environments, especially since I prefer a somewhat outdated OS on my Windows side. (Win7) You have me curious about the PAL issue though, I live in Canada so except for a few live-action anime adaptions all my video backups were muxed from disc with mkvToolNix, and then run through this script. They are mostly NTSC, and mostly 1080P so things look pretty good on my end. Sadly I don't own a 2k TV. How common is 50fps for PAL tracks? Is 25fps the standard? Btw my method DOES drop/copy frames to avoid sync issues.

    EDIT: Perhaps I need a PAL version... or to detect if it's PAL and use the current FPS only if it's NTSC, make 25 fps the default for PAL... Yeah, I can use the MODE variable for that, after I choose mode 1, 2 or 3 and before I run the conversion. Remember, were balancing quality with required processing power, so what's ideal for you might not be ideal for others. I don't own a 2k TV, and I'm stuck on a lame-ass dual-core for now, so it's perfect for me and for others who own outdated systems. Just gotta work out the kinks first. So to clarify, in this instance "optimize" doesn't just refer to video quality, we also have to consider if the video might be too laggy to play on some systems, otherwise I would be using h265 instead. And we also have to consider filesize, I'm juggling 3 different goals here lol

    EDIT2: And the max bitrate is actually 1500, not 1200. I set a maxrate, remember? This isn't QP mode, the syntax is different. Think about it like this: we do 2 passes. the first pass we aren't converting anything, just scanning and generating a stats file with information on the video. We can use that information to later tell if a scene is low-intensity or high-intensity. Our default bitrate is 1500 and the high-intensity scenes get to use the max, 1500, but when we switch to another scene where it's just talking, no special effects (low-intensity) our stats file tells us this scene is low-intensity and so that scene get's converted at 1200 instead, carving out some extra space. 80% of most movies are low-intensity. The switch is instant, I suspect it does the bitrate switch at each scene change, but not necessarily at each GOP, since that data might not exist in some videos. Perhaps the scan picks it up somehow.
    But yeah, I am gonna add that if statement, I'll make the default for PAL videos 25fps in version 1.01B, keep an eye out, I'll post here when it's done.
    Last edited by imkira3; 28th Aug 2025 at 00:44.
    Quote Quote  
  7. Why not simply stick with the frame rate of the input?
    users currently on my ignore list: deadrats, Stears555, marcorocchini
    Quote Quote  
  8. Originally Posted by Selur View Post
    Why not simply stick with the frame rate of the input?
    Because you could potentially save a lot of space depending on the original framerate
    Quote Quote  
  9. What you are doing atm. doesn't really seem like optimization to me, but I wish you the best of luck with your endeavour.

    Cu Selur
    users currently on my ignore list: deadrats, Stears555, marcorocchini
    Quote Quote  
  10. Originally Posted by Selur View Post
    What you are doing atm. doesn't really seem like optimization to me, but I wish you the best of luck with your endeavour.

    Cu Selur
    Like I said, it's a balance between filesize, quality and required resources. Later versions will be called visa-vide system32 or SysWow64 for example, another improvement will be adjustments to default commands based on resolution, but custom commands will be allowed too. See what I mean, you could even use this to mux if you wanted, you will have full control over manipulating the ffmpeg core command, but that's just a very small part of the code. It does other stuff too, to the subtitles, and mkaoptimize downmixes subtitles to stereo, my stereo conversion is different from your typical downmix, I keep all track audio at 100%, that's another good way to save space, but more importantly mkaoptimize can downmix better than most setups that nearly mute rear and LFE tracks. Here, take a look at this preview of the mkaoptimize core command for 5.1 audio:

    :: Downmix (MUX) 5.1 audio to stereo without quality loss
    ffmpeg -i "The Count of Monte Cristo 1080p4.mkv" -af "pan=stereo|c0=FL+FC+LFE+BL|c1=FR+FC+LFE+BR, volume=1.0" -ac 2 "output.wav"

    See how left tracks go on left, right tracks go on right, center goes on both and audio for all tracks defaults to 100%? Sounds like common sense, but apparently not, many speaker systems drop or near-mute some tracks during a downmix.

    EDIT: The current version defaults to WAV to preserve quality, I wouldn't recommend using it that way, the audio track will be huge. I was gonna suggest people use mkaoptimise first initially, then I later decided to change the command to output in AAC instead and make mkvoptimive mux all AAC tracks so order of operations becomes less important.
    Last edited by imkira3; 28th Aug 2025 at 12:56.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!