WMV to MKV Re-encoding Help

savajex

pretty girl lover
Aug 11, 2012
530
1,183
I have a bunch of HQ WMVs that I'd like to re-encode to MKVs of lower quality because they take up way too much HD space. The WMVs have a CBR of 2000 Kbps and 30 fps but ideally I'd like to get it down to around 1000 Kbps.

I use MeGUI and Sharktooth's x264 profile but the audio and video in the resulting MKV are out of sync. And progressively out of sync, not just delay. I've tried demuxing and muxing with MKVmerge, adjusting delay and timecode, but no good.

I've researched this for hours and I can't help thinking I'm doing something really wrong during the encoding process. I'm a complete noob at encoding so if anyone could help me with either WMV->MKV encoding for MeGUI or this audio sync issue, I'll be tremendously grateful! :bow-pray:
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
I'm guessing MeguI is using directshow or something like that to load the wmv which isn't very good at keeping audio in sync for encoding.

If you can find how to specify FFVideoSource for the video and FFAudioSource for the audio in the avisynth script created by MeguI or ffmpegsource2 somewhere in the settings you would have a lot more chances of keeping it in sync.

I don't like to use GUI for encoding since I find that they often lack configurability and add a layer of instability, so I can't really help you with how to achieve that with MeguI.
But if you would be interested in installing avisynth and a few softwares manually, I could modify my usual encoding script so that a simple drag and drop would reencode it.

Let me know if you're interested in the script or not.
You would need to install :Avisynth(and the ffmpegsource2 filter), x264, avs2pipemod, neroAACcodec and mkvtoolnix which you can find links and detailed explanation in the "Auto Encoding to Posting" tutorial in my sig.
 

savajex

pretty girl lover
Aug 11, 2012
530
1,183
Yes! Yesyesyesyesyes! Please! :please:

I'm willing to try anything. I looked at your encoding guide many, many times but it fried my brain each time. And it was from DVD to MKV, so I couldn't figure out how to apply it to my problem.

Btw, if I were to use 10bit x264, would it still take +10 hours to re-encode from WMV? Or was that just because you do it straight from the original source? I don't know if my wimpy dual core laptop could handle it... :scared:
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
Yeah, it's pretty specific and very automated so it's not really a tutorial to learn about encoding in general.

It will take a lot less time to encode since you don't have to deinterlace it which is the very demanding part in that script.
Also, you're better off using the 8 bit version of x264(both work in exactly the same way) if you have a slow pc since the 10 bit version isn't a lot better(you'll save maybe 2-3% with the filesize and probably won't notice the very slight quality boost) and takes a lot longer(about 50% longer I think, I don't really remember) to encode with. I'm only using it because I have a really good pc and I like extreme quality a bit too much.

You can always lower the x264 preset until you're satisfied with the speed, you don't have to use extreme settings like me, but they help a lot in keeping the file size low.

I'll start working on the script, but I'm not sure if I'll have time to finish it before going to sleep since it's getting pretty late. It will be ready tomorrow for sure though.
In the meantime, you can start installing the softwares I listed in my first reply(check the installing the software section of the tutorial I mentioned if you need help and ignore the rest of it) and let me know if you have trouble with them.
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
As if I could have gone to sleep before finishing it.

Here it is: *File removed since it was wrong*

Before you start using it, you need to change a few things inside.
There's 7 lines starting with "SET" at line 16 that you need to change(only what's after the "=") :

x264enc is the path to x264(you need to use the 32bit version).

a2pm is the path to avs2pipemod.

aac is the path to neroAacEnc.

merge is the path to mkvmerge.

x264param is the x264 encoding parameters. The defaults I put there should be decent and not too hard on your computer(I hope), but you can adjust the preset with these from fastest(and biggest file size) to slowest: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo
And you can lower the quality(bitrate) by changing the crf option which is set at 21 by default. A lower number means better quality and lower means less. You can also add decimals, for example 21.5 .
You can also change --crf by --bitrate and set it to 1000 like you wanted, but crf is better since it will choose how much bitrate to give to a scene to keep a certain quality. The inconvenient is that you won't know the final file size until the encoding is done and it might be lower or higher than half of the original(like it would be with half the bitrate) depending on the movie.
Or you can copy some from MeguI, you should be able to see the command line if you edit a preset(I think). Just make sure not to include "x264" at the beginning and the input and output file.

x264threads is the number of threads to use. 4 should be a good default for you, but if your CPU isn't at 100% when encoding, you can try to go higher.

sufftest is what going to be appended at the end of the final encoded filename.(there will also be a _ before it so if you leave it blank, a _ will still be appended to the original filename).


After that, you should be ready to go. Just drag and drop a wmv file(it will also probably work with pretty much anything), enter a title for the mkv(it will be displayed by the player when you play the file) if you want to or press enter without typing anything to not put one.

Let me know if you have any questions or if something isn't working.
 

savajex

pretty girl lover
Aug 11, 2012
530
1,183
Okay! I've been fiddling with it and changed the parameters as you said. I hope you can take a look at it and see if I did it correctly.

But when I tried to run it, it said that it cannot open the _0_config.txt. I'm guessing I need your config file? :puzzled:
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
But when I tried to run it, it said that it cannot open the _0_config.txt.

Oops, my bad, I uploaded the wrong file. That's what happens when you work too late.

Here's the right one that doesn't need the external config file: [attachment gone, see this [post=2358399]post[/post] for the latest version.]

2 things, merge needs to point to mkvmerge.exe and not the gui(mmg.exe) since it will do the merging automatically without asking you anything.

Also, if you simply set the preset without specifying anything else, the quality will default to --cfr 23. It's fine, but I just wanted to let you know.
 
Last edited by a moderator:

savajex

pretty girl lover
Aug 11, 2012
530
1,183
Something's happening! I don't know what, but it's running! :gayprance:

And the wait begins.........

Edited: 8 hour wait?! :surprised: Goodness...
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
If it looks like this then you're on the right track(that's x264 encoding the video track):
[FULLATTACH]656189[/FULLATTACH]

Using the defaults I set except with 8 threads instead of 4, it takes me a bit over an hour for a 2.1GB 1h40 wmv file(the one from that screenshot). I'm only using around 90% of the CPU so using more threads would probably lower that time a little.

You'll have to tweak the settings until you find the mkv is being encoded fast enough and the resulting quality/file size is good enough for you.

Just remember that there's no substitute for time if you want a result of good quality with a low file size. Of course having a better computer always help.

Edit:
That's the audio being encoded:
[FULLATTACH]656195[/FULLATTACH]
And that's the video and audio being merged:
[FULLATTACH]656196[/FULLATTACH]

The result in this case is a 888MB 1h40 mkv file.

Edit 2: To give you a little comparison, if I use the slow preset instead of slower, the file size becomes 927MB.
 

savajex

pretty girl lover
Aug 11, 2012
530
1,183
Thanks for the details. I'm into higher quality even if it takes longer, too, but I worry for my laptop. It's a wee little thing. Thankfully, it only ended up taking 5 hours, though. :exhausted:

Except the audio is still out of sync, like when I used MeGUI. I'm going to try a different WMV file because I've been trying to re-encode the same vid over and over. Will let you know how that goes. If it doesn't, I might have to crack and settle for AVI. :rainyday:

I've also attached the MediaInfos for the WMV and the resulting MKV if it could be of any use to you. And as always, thank you for your patience.
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
I've also attached the MediaInfos for the WMV and the resulting MKV if it could be of any use to you.

It has been of great use since it tells me the number of frames per second(Frame rate) in the mkv is lower than the original wmv for some reason so that would cause the audio to slowly delay more and more. Not sure why it wasn't detected automatically properly though, maybe it's a weird wmv file.

To fix it for this particular wmv, you can set the fps manually by adding the following in the script after: FFVideoSource^("%~f1"
Code:
, fpsnum = 30030, fpsden = 1001
Then make sure the Frames/sec says those 2 numbers in the * rectangle when you start the script like it does in the first screenshot from my previous reply.


Also, I assumed the language of the movie would be japanese since it's a jav forum, but if it's english(like the original wmv says, not sure if it's actually right), you should change SET lang=jpn to SET lang=eng

And Also, again, if you want to get rid of the ffindex file at the end of the encode, add the following at the very end of the script, right before the last )
Code:
 && DEL "%wrkdir%%~nx1.ffindex"
 

savajex

pretty girl lover
Aug 11, 2012
530
1,183
:hi: Don't sleep much, do ya?

I knew there was something with the frame rate! Not so dumb after all :pandalaugh: Of course, I still wouldn't know what to do..

Thanks for the prompt reply! Gonna set that up and tuck in for the night. Hopefully good news in the morning!
 

savajex

pretty girl lover
Aug 11, 2012
530
1,183
IT WORKED. :gayprance:

:bow-pray::bow-pray::bow-pray::bow-pray::bow-pray:

Thank you so much! Got a 1.32 GB vid down to 817 MB.

So in the future, I should watch for the framerate? I just started re-encoding another WMV with 25 fps but the batch file turned it into 24.997 fps (25022/1001). I edited the batch so that it would be 25025/1001, the original 25 fps, and restarted the encoding. That's okay, right?
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
Glad to see everything worked and yes, that's exactly right as long as you're sure the FPS in the original is really exactly 25.

The most common should be 24000, 25025, 30000 and 30030(all divided by 1001) but there's always weird ones out there, especially in wmv files.

I display the 3 things(total frames, FPS and the resolution) before the encodes to help prevent possible mistakes by the automation in the script or human errors, but there is a lot less chances of those in this case with only a drag and drop.

I never had a wrong framerate detected myself so far, but I only tried with 3 different wmv's. That format is probably the least well supported by encoding tools so being vigilant is always a good idea.

I suggest you duplicate the FFVideoSource line in the script to have one with the FPS set manually and another without and add REM followed by a space(it will become a comment and be ignored by the script) at the beginning of the one you're not currently using.

It should be able to detect it properly most of the time unless there's something wrong in the ffms2 build you downloaded(which would be unlikely though) so if it persist in detecting it wrong, you can try updating ffms2 when a new version comes out.

Edit: Since I was curious, I tried every presets on the same video to see what the file size would be and here's the result:
Original video: 2.1GB
ultrafast: 1.97GB
superfast: 1.10GB
veryfast: 845MB(that one is really odd since it took about as much time to encode as the veryslow preset. Not sure why)
faster: 951MB
fast: 979MB
medium: 947MB
slow: 927MB
slower: 888MB
veryslow: 818MB
placebo: 839MB
 
  • Like
Reactions: nin1

savajex

pretty girl lover
Aug 11, 2012
530
1,183
I suggest you duplicate the FFVideoSource line in the script to have one with the FPS set manually and another without and add REM followed by a space(it will become a comment and be ignored by the script) at the beginning of the one you're not currently using.

At risk of sounding really simple, oooh, that's clever. :bingo: Done.

And I can't believe you actually tried all those settings. That's insane. :dizzy: I can't believe the difference between ultrafast and superfast is so huge.

Thanks again for everything. (I might never stop thanking you, just saying... :grassdance:) All right, time to encode and upload!
 

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
Here's a new version that support splitting the encode and has basic fps detection in case it's not detected right.

As usual, drag and drop a video on the bat file and follow what the screen tells you.
If you choose to split the encode, you'll have to run the bat file without dragging and dropping the video on it(after you've ran it once by drag and dropping a video on it) and it will ask you which part you want to encode(you only need to enter the number).
It's important to encode the last part last if you want the whole video to properly get appended and merged automatically.

Here it his: [attachment gone, see this [post=2358399]post[/post] for the latest version.]
 
Last edited by a moderator:

hovhaness

Active Member
Feb 1, 2008
112
239
I use handbrake with an RF setting of 22
Mkv is trendy but unless you are looking to use AC3 6 channel audio or to add multi-subs then there is really no reason at all to use an mkv container.
If it is just a straight ahead encode.
I would go for either an MP4 or M4V container because it is easy to play on most any device.
Just couple this with AAC audio.
You will use the same h.264 codec being used for mkv, but mp4 is more widely supported.
Handbrake is also blazing fast.
If you insist on MKV container Handbrake does a great job there as well.
Here is the result of me going with a 1 pass RF22 setting & encoded from a source that was 2000kbps & see now it is trimmed to just under 1/2 size for the video at 992kbps.
Look at the quality. Very nice for the size.
This is a scene with a lot of motion so it is where you would see many artifacts & problems with a bad encode.

http://www.sendspace.com/file/lyq96v

Just google Handbrake & there is very easy instructions right on the site.
They have presets for devices built in & it will make your life a lot easier.

Here are the specs
Good Luck.

General
HDTYD.sample.mp4
Format : MPEG-4
Format profile : Base Media / Version 2
Codec ID : mp42
File size : 16.6 MiB
Duration : 2mn 0s
Overall bit rate mode : Variable
Overall bit rate : 1 157 Kbps
Encoded date : UTC 2013-09-15 06:53:14
Tagged date : UTC 2013-09-15 06:53:30
Writing application : HandBrake 0.9.8 2012071700

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : Main@L3.0
Format settings, CABAC : Yes
Format settings, ReFrames : 4 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 2mn 0s
Bit rate mode : Variable
Bit rate : 992 Kbps
Width : 720 pixels
Height : 406 pixels
Display aspect ratio : 2.35:1
Frame rate mode : Variable
Frame rate : 23.976 fps
Minimum frame rate : 18.770 fps
Maximum frame rate : 23.981 fps
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.142
Stream size : 14.2 MiB (86%)
Writing library : x264 core 120
Encoding settings : cabac=1 / ref=1 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=2 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=240 / keyint_min=24 / scenecut=40 / intra_refresh=0 / rc_lookahead=10 / rc=crf / mbtree=1 / crf=22.0 / qcomp=0.60 / qpmin=3 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Encoded date : UTC 2013-09-15 06:53:14
Tagged date : UTC 2013-09-15 06:53:30
Color primaries : BT.601 NTSC
Transfer characteristics : BT.709
Matrix coefficients : BT.601

Audio
ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Format profile : LC
Codec ID : 40
Duration : 2mn 0s
Bit rate mode : Variable
Bit rate : 160 Kbps
Maximum bit rate : 190 Kbps
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 44.1 KHz
Compression mode : Lossy
Delay relative to video : 95ms
Stream size : 2.29 MiB (14%)
Encoded date : UTC 2013-09-15 06:53:14
Tagged date : UTC 2013-09-15 06:53:29
 
Last edited by a moderator:

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
Handbrake is simply a graphical interface for the same softwares(x264 at least, it doesn't say what was use for aac in mediainfo) I use in the script I made and also that Megui use(and pretty much any free h264 encoding app out there since x264 is the best one and is even used for some retail bluray's) which means more things can go wrong since it's more complex.

Does AC3 6 channel audio really not work in mp4s? I was under the impression that it did and AAC 6 channels certainly does.
MKV has much more interesting features over mp4 than the 2 you mentioned, like ordered chapters, file linking, editions, default/forced tracks, more metadata like including the title of the movie and probably other stuff that I can't think of right now.

For the purpose of this script though, it would indeed be better to use mp4 since it's a simple conversion that won't use any fancy features(except maybe including the title, I can't remember if I put that in there) and it has better compatibility when used outside of a computer.
I didn't simply because I adapted my much more complex script for this simple task and I use some of the fancy features in it and use 10bit so compatibility is irrelevant since it won't have any either way. It would probably be a good idea to use mp4 if I update it again(I should at least update the link for it in this thread in the near future).

RF 22 in handbrake is simply the crf option for x264. I think I use 21 by default, but 22 would probably also work fine for most blurry wmv around here.

One thing you should keep in mind is that you can't rush quality. The faster you go, the lower the quality or the bigger the final file size will be. There's rarely a way around that, especially not for x264 which is very efficient.

Looking at the encoding settings line, I see that you're using the "veryfast" preset for x264. Something was odd last time I tested it(you can look at the presets results in post 14. I'll have to properly benchmark them sometimes), but you'd probably get a smaller file size using preset higher than medium.

Edit: Another thing I just noticed is that you use variable framerate for the encode which help making the size smaller, but also pretty much destroy compatibility.
 
Last edited by a moderator:

SamKook

Grand Wizard
Staff member
Super Moderator
Uploader
May 10, 2009
3,525
4,877
Re-encoding script v2.16

I've made yet again a few modifications to bring it to my own standard of use so here's a new version with some new stuff.

Overview:
This script can re-encode many video files to either a mkv or mp4 file with h264 video and aac audio.

For it to work, you need to install the following:
- avisynth(v2.6 recommended) which you can get here: http://forum.doom9.org/showthread.php?t=172213
If you want the MT version, first install the official 2.6(link above) and then replace the avisynth.dll in you windows\syswow64(or system32 if you have a 32bit OS) with this one: http://forum.doom9.org/showthread.php?t=148782
- The ffmpegsource2(v2.18-rc1 or better recommended since it won't crash with video that contain glitches) plugin for avisynth which you can get here: https://github.com/FFMS/ffms2/releases
You need to extract FFMS2.avsi and the x86 ffms2.dll in the plugins folder of the avisynth installation.
- x264 32bit(x86) which you can get here(get it from the "clear" column): http://komisar.gin.by/
- avs2pipemod which you can get here(There might be a newer version later in the thread. v0.4.2 is experimental so it might be safer to stick with v0.4.1): http://forum.doom9.org/showthread.php?p=1565165&postcount=116
- neroAACcodec: http://www.videohelp.com/tools/Nero-AAC-Codec
- mediainfo(the CLI version): https://mediaarea.net/en/MediaInfo/Download/Windows
- mkvtoolnix(which is only required if you want to output to mkv): http://www.bunkus.org/videotools/mkvtoolnix/downloads.html#windows
- mp4box(which is only required if you want to output to mp4): http://www.videohelp.com/software/My-MP4Box-GUI
- timer(which is only required if you want to log the time it takes to encode): http://www.gammadyne.com/cmdline.htm#timer

The full list of features/changelog is:
v2.16
- It will now double check the FPS using mediainfo since some formats were very unreliable(mostly wmv and mpg files).
v2.15
- Added the option to pre-answer most of the questions asked by the script.
v2.14
- Added support for as many input files as the command line can take(which is a lot).
- Added the option to re-encode only the audio using a different source filter in case it's not done properly the first time(it happens sometimes with some wmv) by simply drag and dropping the source video again(but it will only be possible if nocleaning is set to y since it needs the temporary files to work).
- The current dimension of the video is now shown when asking to resize.
- Fixed a bug where 25 and 30 FPS video were detected as weird framerate when they shouldn't be.
v2.07
- Added support for uncompressed audio track over 4GB to prevent the audio encoding from crashing when the audio is long or 5.1 audio is used.
- Changed the amount of possible input video from 5 to 9 because why not, but more than that would be problematic for a batch file.
v2.05
- You can log the time it takes to encode the videos.
- You can choose to get asked to resize a video if its width is bigger than a predefined number(900 by default to only ask for HD movies).
v2.02
- You can enable multithreading when loading the source video(mostly useful if you have a slow pc) and choose the number of threads.
v2.0
- Can take from 1 to 5 input video from either drag and drop or via command line as parameters to the .bat file.
- Can split the encode in as many parts as you want if you don't want to encode it in one go.
The last part need to be the last one encoded or it will not get merged properly in the end.
- Can encode to either a mkv or mp4 file with the option to be asked or not.
You will only be asked when encoding the last part in the case of a split encode.
- If the framerate isn't a common one or if it can't be automatically detected, you'll be prompted to change it or keep it as is.
- If there's more than one input file, you'll be asked to confirm that they're in the right order.
When drag and dropping and all desired files are selected, you need to click and drag the first file for them to be in alphabetical order.
- You'll be prompted to enter a title for mkv files which will be omitted if you enter nothing.
- You can change the default x264 settings which are set in a variable to make things easy.
- The script can be paused at the right time if you want to manually edit the .avs file(if you know what you're doing) by setting a variable.
- The temporary files used during the encode can be deleted or kept by setting a variable or not.
- You can use the name of the current folder or the first file for the final encode name by setting a variable or not.
- The language of the video and audio track can be set(Japanese for both by default) to anything you want by changing a few variables.
- You'll only get prompted for things that applies to your choices. If that isn't the case, please let me know.

How to use it:
- Modify the paths and set the desired options in the "User config" section at the beginning of the .bat file using a plain text editor like notepad2 for example.
The lines starting with REM are commentaries that explain the options.
- Copy the .bat file in the same directory as the movie you want to re-encode.
- Drag and drop the movie on the .bat file and follow the directives. The final file name will end with "_final".

Known bugs:
-FFMS2 v2.18 RC1 and v2.19 sometimes(always?) hang when trying to load an HD video. The solution is to use v2.17(or a newer release when it comes out) for them.
update:It seems to be fixed in v2.20 and I haven't tried 2.21 yet but I assume it would work even better.

-FFMS2 v2.20(and possibly other versions) has trouble re-encoding the audio for some videos. The solution is to disable the cleaning of the temporary files in the script(set nocleaning to y if it's not already since it's the default) and drag and drop the source again. Then press d followed by enter when asked to use directshow instead for the audio(it will only re-encode the audio which is pretty quick and not the whole video).

Download:
View attachment _re-enc_v2.16.rar
v2.16 mirror: http://www.mediafire.com/?ur31jubhacrjpld

Old versions:
View attachment _re-enc_v2.15.rar
v2.15 mirror: http://www.mediafire.com/?vrpp28zz5a1dy52

View attachment _re-enc_v2.14.rar
v2.14 mirror: https://www.mediafire.com/?i3nifkuxzraf2uw

View attachment _re-enc_v2.07.rar
v2.07 mirror: http://www.mediafire.com/?40t1l06ao4q0gkw

View attachment _re-enc_v2.05.rar
v2.05 mirror: http://www.mediafire.com/?k4bzc0v334024ag

View attachment _re-enc_v2.02.rar
v2.02 mirror: http://www.mediafire.com/?c1h28ywnyn57h7f

View attachment _re-enc_v2.rar
v2 mirror: http://www.mediafire.com/?hmml5z6md6qv2xv
 
Last edited:

odonkor

Member
Jul 9, 2013
83
61
hey SK, i have a question
i want to convert a files from 2 gb to 500 mb or 700 mb without lowering the audio video quality, i use Any video converter but the quality is bad can you help me?