# What settings when converting video

1. Regarding muxing to MKV: MkvMerge doesn't unpack the packed bitstream so the resulting video may not play well on all devices.
Alright then. Even the most recent version ? (It gets updated quite often.{*}) Is there a specific reason for that ? And what is the purpose of packed bitstream to begin with ? (Perhaps I'll remember next time around...)

{*} I currently have v. 35 which is more than a year old, but I like its name, and it works fine so I didn't feel the urge to upgrade ; now it's already v. 51 called “I wish”. I wonder where those fancy names come from. Could it be from songs, since “I wish” is a classic tune from Stevie Wonder ? (Before that they had : “Sick of losing soulmates”, “Fortress around your heart”, “The silent type”, “The Quartermaster”, “No deeper escape”, “The flower kings”, “Awakenings”... Perhaps it all adds up to form a cryptic message from the Illuminati...)
2. Packed bitstream (sometimes called the "virtualdub hack") is a workaround for out-of-order codecs and Microsoft's VFW library for reading AVI files. VFW is based on a one-frame-in-one-frame out model. It reads one frame worth of compressed data, decompresses it, then sends it to the player (or editor). That doesn't work with out-of-order codecs because the frames aren't stored in the order they're displayed. Consider a sequence like IBBP. That's four frames of video in their display order, frames 1,2,3,4. The I frame (key frame) is encoded much like a JPEG image -- all the compressed data that's needed to reconstruct that frame is included in that frame. The last frame of the sequence, the P frame (4, a "Predicted" frame) may use parts of the first frame and its own compressed data to reconstruct the full frame. So the I frame has to be decoded and accessible before the P frame can be decoded. The two B frames ("Bidirectionally predicted" frames) between them may reference parts of the I frame and/or the P frame. So both the I frame and the P frame must be decoded before either of those frames can be decoded. To reduce the amount of seeking (very important for slow seeking devices like CD-ROM or DVD-ROM) the frames are store in the order IPBB (1,4,2,3). At playback the frames are decoded in that order but they are rearanged on the fly and displayed in the correct order IBBP (1,2,3,4).

To get that working with VFW the BBP sequence is stored in the AVI file as if it's one frame (hence "packed"). Then to make up for the missing frames two NULL frames are included in the AVI file: I(PBB)NN. At playback the decoder first decompress the I frame (1) and presents it to the player. Then it decompress the entire PBB sequence (P, then B and B, all stored internally) and presents only the first B frame (2) to the player. When the player asks for the first NULL frame the decoder presents the second B frame (3) which has already decoded. Finally, when the player asks for the second NULL frame the P frame (4, already decoded) is given to it.

Out of order codecs have to be specially written to support this convoluted method. Divx/Xvid/x264 all support it.
3. Originally Posted by abolibibelot
It's kinda reassuring, goes to show that I'm not alone in this hectic world who can be vvveerrrrryyy sssslllooooow at doing things sometimes...
A few “capsule replies” :
– You should learn how to take screenshots.
– Instead of going through all that hassle, just because the stupid new TV dropped support for older files that were played fine on the older model from the same brand, you could get a cheap standalone player that is known to play all common digital media file formats, like the once popular WD Live. Never had one, but I've read nothing but praise about that particular unit, it was quite cheap new and could be found even cheaper now used. Don't know if it's been updated to play newer formats like H.265, but that's not the issue here. Or play the pesky files from a laptop computer, or a Raspberry thingy (no experience with these), or some Internet set-top media boxes...
– If converting those pesky AVI files is really necessary, perhaps mkvmerge wouldn't have hiccups like ffmpeg does. It can be used from a GUI called MKVToolNix, but to process an entire folder a CLI script is way faster. What's nice is that when importing a file (drag-and-drop works) into MKVToolNix, then changing any setting, it can display the actual command line that is sent to the mkvmerge.exe executable ; then based on that it's easy to create a FOR ... DO ... loop script, even without fully understanding the intricacies of each and every switch / option involved. Anyway, for a simple AVI to MKV remuxing the command is quite straighforward.
Code:
"C:/Path/to/MKVToolNix\mkvmerge.exe" --ui-language en --output ^"E:\Path\to\output\output.mkv^" --language 0:und --language 1:und ^"^(^" ^"E:\Path\to\input\input.avi^" ^"^)^" --track-order 0:0,0:1
(Not sure why it replaces some backslash characters by forward slash characters, or why it adds those seemingly superfluous ^"^(^" ^" parentheses and escape characters.)
That's the base command obtained from “Multiplexer” => “Show command line” ; then the loop script would be (removed the unnecessary clutter) :
Code:
FOR %%F in (*.avi) DO "C:\Path\to\MKVToolnix\mkvmerge.exe" --output "%%~nF.mkv" "%%F" --track-order 0:0,0:1
Tested with an AVI file which has the “packed bitstream” kind of muxing (don't remember exactly what that is, I've probably read an in-depth explanation at some point but quickly forgot) : works fine, very quick. With “FOR /R” (/R stands for “recursive”) an entire folder with all its subfolders could be processed at once. It also allows to set a working directory different from the one where the .bat file is located, for instance if the .bat file is in E: root directory, this will process every AVI file in "F:\VIDEO" and all subdirectories :
Code:
FOR /R "F:\VIDEO" %%F in (*.avi) DO "C:\Path\to\MKVToolnix\mkvmerge.exe" --output "%%~nF.mkv" "%%F" --track-order 0:0,0:1
The same command can be launched from the command prompt, in which case only one “%” sign should be used for the arguments.
Code:
FOR /R "F:\VIDEO" %F in (*.avi) DO "C:\Path\to\MKVToolnix\mkvmerge.exe" --output "%~nF.mkv" "%F" --track-order 0:0,0:1
Another way is to first specify the working directory with “CD” (change directory), then execute the actual command.
Code:
F:
CD "VIDEO"
FOR %%F in (*.avi) DO "C:\Path\to\MKVToolnix\mkvmerge.exe" --output "%%~nF.mkv" "%%F" --track-order 0:0,0:1
Well, let's hope this gets solved before June 2021 ! (I'm talking about the Covid of course.)

I only did a couple of files and only tested it on my Samsung TV but before I delete the original AVI files I would like to feel confident it will play on other players with no problem, as once I delete the files won’t be able to try anything else if I find problems on different players.
I would be wary about deleting the original files, especially when it comes to personal recordings mentioned on page 1. Hence my suggestion of getting a complimentary standalone device that can read all that stuff with no fuss.
I assume there is something wrong with my screenshots? I use Microsoft Snipping Tool, what would you recommend?

I do have the WD Player and also two of the Seagate ones (can't remember what they are called anymore). It seems there is some problem with each of the players I have tried. The other problem is I have about 8 devices that are HDMI and 3 HDMI ports on my TV so between using some inputs on my home theatre system and a 5 port hub/switch it is a pain using some of these devices and as I mentioned most have issues of one type or another. They are probably fine if you don't have many files but are very cumbersome otherwise. I also have a Sony I think it was called Internet Player or something similar. It was around $200-$250 and it doesn't recognize a hard drive larger than I believe it was either 500GB or maybe 1TB so it doesn't even see my hard drives. Talked to Sony numerous times and was continually assured they would update the player so it would see my drives but never has and I don't think they support it anymore. I have used the player on the Roku and it doesn't work that well either. I have VLC on an Amazon Fire TV stick and again plays some stuff not bad but other stuff is jerky. I like the player built into the Samsung TV as it is handy and don't have to play around to access the drives just unfortunate they dropped support for formats/codecs they once supported. It isn't like they need to add hardware that would increase the cost of the TV, just keep what they already had and add new formats to it.

I'll have a look at the rest of the reply after Christmas as will be busy again the next week or two.

I do have backups of backups of personal recordings/files so would not delete the originals under any circumstances. I am paranoid about that so have a few backups of all of those files.
Thanks again
4. Originally Posted by jagabo
Originally Posted by jackdup
Originally Posted by jagabo
Whichever line is causing the problem. I can't tell from your screen shot. Or both lines. It just supresses the less critical warning messages.
I have had so much going on believe it or not I didn't get around to trying this until today. It has taken roughly an hour to do 18 files which seems a lot longer than I understood this should take.
I just ran a test with 24 Xvid AVI files of about 650 MB each (~52 minute TV episodes), 14.5 GB total, on a 4 TB local hard drive. It took about 6 minutes and 30 seconds. CPU usage was only about 5 percent (8 core, 16 thread system). But if you have lots of files that were generating lots of error messages it may take much longer. Even though ffmpeg isn't writing those error messages to the screen it's probably still doing a lot of extra work internally.

Originally Posted by jackdup
Here is the contents of the batch file so would appreciate if you would look it over and insure it is correct and that there isn't an error or something which would be causing it to take longer than it otherwise would.

Thank you

Code:
for %%F in ("*.avi") do (
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nxF" -c copy -bsf:v mpeg4_unpack_bframes "%%~nF.temp.avi"
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nF.temp.avi" -c copy -bsf:v mpeg4_unpack_bframes "%%~nF.mp4"
del "%%~nF.temp.avi"
)
It's my mistake but the second ffmpeg command line doesn't need the " -bsf:v mpeg4_unpack_bframes" option. It's already been done by the first command. I don't think it makes much difference in the execution time though.

Originally Posted by jackdup
Perhaps I could put each command into a separate batch file and see which of the two commands is taking the majority of the time or perhaps they are taking an equal amount of time.
It's worth a try.
I'll try removing that section and try it and see if it makes a difference. I would prefer staying with mp4 rather that using mkv. While I would prefer something a bit quicker it really doesn't matter that much as it does an entire directory without any babysitting. I have tried playing the mp4 files on several different players but what I have tried plays fine which is more important that the time it takes to do the files.

Thanks again for your help and will spend a bit more time reviewing yours as well as the other posts after the new year.
5. I assume there is something wrong with my screenshots? I use Microsoft Snipping Tool, what would you recommend?
Weren't they pictures taken with a camera ?

I do have the WD Player and also two of the Seagate ones (can't remember what they are called anymore). It seems there is some problem with each of the players I have tried. The other problem is I have about 8 devices that are HDMI and 3 HDMI ports on my TV so between using some inputs on my home theatre system and a 5 port hub/switch it is a pain using some of these devices and as I mentioned most have issues of one type or another. They are probably fine if you don't have many files but are very cumbersome otherwise. I also have a Sony I think it was called Internet Player or something similar. It was around $200-$250 and it doesn't recognize a hard drive larger than I believe it was either 500GB or maybe 1TB so it doesn't even see my hard drives. Talked to Sony numerous times and was continually assured they would update the player so it would see my drives but never has and I don't think they support it anymore. I have used the player on the Roku and it doesn't work that well either. I have VLC on an Amazon Fire TV stick and again plays some stuff not bad but other stuff is jerky. I like the player built into the Samsung TV as it is handy and don't have to play around to access the drives just unfortunate they dropped support for formats/codecs they once supported. It isn't like they need to add hardware that would increase the cost of the TV, just keep what they already had and add new formats to it.
That's messy indeed... é_è Manufacturers obviously don't care about long term support and don't want to bother updating a model that's more than 2 years old, as it's more profitable to keep on churning newer ones.
What are the issues with the WD player ? Is it the “WD Live” or another ? When was the last time it got updated (if it did get updated at all) ?
Does anyone know what could justify the dropping of formats that were decoded by former models of the same brand ? Cost of patents perhaps ?

I would prefer staying with mp4 rather that using mkv. While I would prefer something a bit quicker it really doesn't matter that much as it does an entire directory without any babysitting. I have tried playing the mp4 files on several different players but what I have tried plays fine which is more important that the time it takes to do the files.
Why MP4 ? MKV container is much more tolerant with regards to which video/audio formats are allowed (plus subtitles, chapters and whatnot). Of course standalone devices are more restricted when it comes to which formats they can read, so perhaps it's preferable to stick with one combination that's known to work flawlessly.
6. Originally Posted by jagabo
Originally Posted by jackdup
Originally Posted by jagabo
Whichever line is causing the problem. I can't tell from your screen shot. Or both lines. It just supresses the less critical warning messages.
I have had so much going on believe it or not I didn't get around to trying this until today. It has taken roughly an hour to do 18 files which seems a lot longer than I understood this should take.
I just ran a test with 24 Xvid AVI files of about 650 MB each (~52 minute TV episodes), 14.5 GB total, on a 4 TB local hard drive. It took about 6 minutes and 30 seconds. CPU usage was only about 5 percent (8 core, 16 thread system). But if you have lots of files that were generating lots of error messages it may take much longer. Even though ffmpeg isn't writing those error messages to the screen it's probably still doing a lot of extra work internally.

Originally Posted by jackdup
Here is the contents of the batch file so would appreciate if you would look it over and insure it is correct and that there isn't an error or something which would be causing it to take longer than it otherwise would.

Thank you

Code:
for %%F in ("*.avi") do (
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nxF" -c copy -bsf:v mpeg4_unpack_bframes "%%~nF.temp.avi"
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nF.temp.avi" -c copy -bsf:v mpeg4_unpack_bframes "%%~nF.mp4"
del "%%~nF.temp.avi"
)
It's my mistake but the second ffmpeg command line doesn't need the " -bsf:v mpeg4_unpack_bframes" option. It's already been done by the first command. I don't think it makes much difference in the execution time though.

Originally Posted by jackdup
Perhaps I could put each command into a separate batch file and see which of the two commands is taking the majority of the time or perhaps they are taking an equal amount of time.
It's worth a try.
I can’t believe it has been a year and a half but have had a bunch of other stuff on the go so haven’t had the time to do anymore with this. I just wanted some clarification on the change you recommended as I know syntax is important. You mentioned the second command line doesn’t need " -bsf:v mpeg4_unpack_bframes". Do I leave the quotation mark before the -c copy and leave the -c copy as well as there is no other quotation mark before the section you mentioned removing. If you don’t mind perhaps you could edit the appropriate line and repost it and them I’ll be sure not to mess it up.

Once again thanks for your help and sorry it has been so long.
7. Change the second ffmpeg line to:

Code:
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nF.temp.avi" -c copy "%%~nF.mp4"
8. Originally Posted by jagabo
Change the second ffmpeg line to:

Code:
ffmpeg -y -loglevel panic -fflags +genpts -i "%%~nF.temp.avi" -c copy "%%~nF.mp4"

Excellent, thank you
9. Originally Posted by jackdup
Originally Posted by jagabo
If you can live with MKV files the easiest thing for you to do is use MakeMKV. It simply remuxes the DVD contents into an MKV file. It doesn't reencode anything so there are no choices to make and it's very fast. The files will be about the same size as your DVDs but there's no quality loss (and no quality gain either).
That sounds exactly what am looking for at least for the AVI conversions but would prefer something that converts to MP4 as some of the video playback controls on the TV don't work on MKVs like they do on MP4 but maybe I will download it and try it.

Thanks for the suggestion
Once you've made an MKV file from the disc, all you need to do is to do this:

ffmpeg.exe -i in.mkv -vf "scale=in_range=full:out_range=full,lut='y=(va l-16)*1.067',yadif=1" -c:v libx264 -crf 17 -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range 2 -c:a copy "out (full range+recode+yadif).mp4"

Full explanation:
10. Originally Posted by rgr
Originally Posted by jackdup
Originally Posted by jagabo
If you can live with MKV files the easiest thing for you to do is use MakeMKV. It simply remuxes the DVD contents into an MKV file. It doesn't reencode anything so there are no choices to make and it's very fast. The files will be about the same size as your DVDs but there's no quality loss (and no quality gain either).
That sounds exactly what am looking for at least for the AVI conversions but would prefer something that converts to MP4 as some of the video playback controls on the TV don't work on MKVs like they do on MP4 but maybe I will download it and try it.

Thanks for the suggestion
Once you've made an MKV file from the disc, all you need to do is to do this:

ffmpeg.exe -i in.mkv -vf "scale=in_range=fullut_range=full,lut='y=(va l-16)*1.067',yadif=1" -c:v libx264 -crf 17 -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range 2 -c:a copy "out (full range+recode+yadif).mp4"

Full explanation:
So would this be preferable to what Jagabo recommended?
11. Originally Posted by jackdup
So would this be preferable to what Jagabo recommended?
1. Performs deinterlacing.
2. Recode to H.264 - MPEG stream in MP4 file is not widely supported
3. You can add crop to the command line (but if the source is VHS, the DVD hardly had black edges).
4. Should improve the contrast, but that depends on what the source is.
5. It will last longer

Just test
12. Originally Posted by rgr
Originally Posted by jackdup
So would this be preferable to what Jagabo recommended?
1. Performs deinterlacing.
2. Recode to H.264 - MPEG stream in MP4 file is not widely supported
3. You can add crop to the command line (but if the source is VHS, the DVD hardly had black edges).
4. Should improve the contrast, but that depends on what the source is.
5. It will last longer

Just test
Most of the files I am converting are avi which I would like to convert to mp4 or mkv. In order to use the ffmpeg you provided I would have to first convert the avi to mkv and then run your ffmpeg if I understand correctly?

Thank you
13. Originally Posted by jackdup
Most of the files I am converting are avi which I would like to convert to mp4 or mkv. In order to use the ffmpeg you provided I would have to first convert the avi to mkv and then run your ffmpeg if I understand correctly?
No, that's not necessary. You can just change the command line he gave you to use the AVI file instead of an MKV file as input.

Code:

ffmpeg.exe -i in.avi etc
His procedure will reduce the quality of your video and take much longer as the video will be re-encoded.
14. Originally Posted by jackdup
Originally Posted by rgr
Originally Posted by jackdup
So would this be preferable to what Jagabo recommended?
1. Performs deinterlacing.
2. Recode to H.264 - MPEG stream in MP4 file is not widely supported
3. You can add crop to the command line (but if the source is VHS, the DVD hardly had black edges).
4. Should improve the contrast, but that depends on what the source is.
5. It will last longer

Just test
Most of the files I am converting are avi which I would like to convert to mp4 or mkv. In order to use the ffmpeg you provided I would have to first convert the avi to mkv and then run your ffmpeg if I understand correctly?

Thank you
No, that's for mkv files from DVDs. For avi it ​​depends on what is in the avi file, but generally:

ffmpeg -i in.avi -c:v libx264 -crf 17 -pix_fmt yuvj420p -c:a aac -b:a 256k out.mp4

(libx264 - codec h264, crf 17 for very high quality (19 is considered as high quality, 22-23 - good quality), pix_fmt for full color range, aac 256k for high quality audio)
15. Also note that virtually all commercial video is limited range (DVD, Blu-ray, all streaming services, etc.). If you use full range (flagged or not) you are asking for trouble. Many players, especially outside a computer, will pay no attention to the full range flag. You may end up with crushed blacks and brights.
16. Originally Posted by jagabo
Also note that virtually all commercial video is limited range (DVD, Blu-ray, all streaming services, etc.). If you use full range (flagged or not) you are asking for trouble. Many players, especially outside a computer, will pay no attention to the full range flag. You may end up with crushed blacks and brights.
So I would be better using the one that you gave me above?
Thank you
17. Originally Posted by jagabo
Also note that virtually all commercial video is limited range (DVD, Blu-ray, all streaming services, etc.). If you use full range (flagged or not) you are asking for trouble. Many players, especially outside a computer, will pay no attention to the full range flag. You may end up with crushed blacks and brights.
I have not seen any modern player that does not support full range. And it will not destroy any ranges, because they will remain in the file - at most an old player will reproduce a worse picture.

My Panasonic DVD Recorder saves the image in the range 16-254, but does not put any range information in the file.
Normal conversion will then destroy the white ranges.
Therefore, first of all, you should check (e.g. in Vegas) the actual color range.
18. Originally Posted by jackdup
So I would be better using the one that you gave me above?
Thank you
MediaInfo (or MPC-BE, MPC-HE, VLC etc.) -> check the "Color range" parameter.
If doesn't exist, check the actual color range (for example, the Vegas program).
19. Originally Posted by rgr
Originally Posted by jackdup
Originally Posted by jagabo
If you can live with MKV files the easiest thing for you to do is use MakeMKV. It simply remuxes the DVD contents into an MKV file. It doesn't reencode anything so there are no choices to make and it's very fast. The files will be about the same size as your DVDs but there's no quality loss (and no quality gain either).
That sounds exactly what am looking for at least for the AVI conversions but would prefer something that converts to MP4 as some of the video playback controls on the TV don't work on MKVs like they do on MP4 but maybe I will download it and try it.

Thanks for the suggestion
Once you've made an MKV file from the disc, all you need to do is to do this:

ffmpeg.exe -i in.mkv -vf "scale=in_range=fullut_range=full,lut='y=(va l-16)*1.067',yadif=1" -c:v libx264 -crf 17 -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range 2 -c:a copy "out (full range+recode+yadif).mp4"

Full explanation:
I copied and pasted this into a batch file and ran it on a few mkv files I have and this was the result.
20. First you need to have your Command Prompt set to where your ffmpeg folder is located.
Mine is in the Program Files Folder.
So I need to:
Code:
cd \Program Files\ffmpeg\bin
You need to tell ffmpeg exactly where the files are on your OS.

Try this simplest code:
Code:
ffmpeg -i input.mkv -c copy output.mp4
It will look more like this:
Code:
ffmpeg -i C:\Users\Users\Videos\input.mkv -c copy C:\Users\Users\Videos\output.mp4

or the slightly more complex:
Code:
ffmpeg -i input.mkv -c:v copy -c:a copy -sn output.mp4
Same as the simpler code you have to tell ffmpeg exactly where the file is on your OS.
21. Originally Posted by jackdup

I copied and pasted this into a batch file and ran it on a few mkv files I have and this was the result.
Replace in.mkv with your file name :)

Code:
ffmpeg.exe -i your_file_name.mkv -vf "scale=in_range=full:out_range=full,lut='y=(val-16)*1.067',yadif=1" -c:v libx264 -crf 17 -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range 2 -c:a copy "out (full range+recode+yadif).mp4"
But in the batch file, the line must look different:

Code:
ffmpeg.exe -i "%%~nxF" -vf "scale=in_range=full:out_range=full,lut='y=(val-16)*1.067',yadif=1" -c:v libx264 -crf 17 -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range 2 -c:a copy "%%~nF.mp4"

Statistics