Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Announcements and Chat > General Discussion

Reply
 
Thread Tools Search this Thread Display Modes
Old 20th January 2019, 22:21   #1  |  Link
Travillion
Registered User
 
Join Date: Aug 2018
Posts: 4
Cutting on IDR frames in mkvmerge

Hello all,

I thought this would probably go better in the editing sub-forum but couldn't find an appropriate category as it doesn't related to avisynth or NLE, etc. So I placed it here.....

I am trying to cut out sections from an h264 source. I understand I need to cut on the IDR frames so I extracted the raw file, ran it through FrameCounter to identify the IDR frames, and selected the appropriate frames. To test my identification of IDR frames, I cut each desired segment out as an individual file and checked playback. Each file played perfectly. To further test the cuts were appropriate, I combined them and they played seamlessly as a single file. All this was done in mkvmerge command line, so no re-encoding is occurring.

Now, I want to do the inverse. In other words, instead of keeping the sections as separate files, I want to discard those portions and leave the remainder of the film intact. But when I do this, using those same ranges, the film now has playback errors (a/v sync issues, black screen with ongoing audio, etc.). So, for whatever reason, I am extracting on IDRs, but I am not discarding on IDRs, if that makes sense.

Here are some concrete details:
Sections (note, each range below begins and ends on an IDR frame):
56075-56195
77926-77950
80223-80343

To extract the sections, I use: mkvmerge -o output.mkv --split parts-frames:56073-56193,77924-77948,80221-80341 input.mkv
(The frames referenced here are each -2 from the IDR frames identified above because mkvmerge begins counting frames at 1 and FrameCounter begins at 0, so I subtract 2 in order to be safe.)
As I said above, this works flawlessly. The individual files play great and they can be merged with no issues, because they are each an independent GOP.

To try and discard the sections, I use: mkvmerge -o output.mkv --split parts-frames:-56073,+56193-77924,+77948-80223,+80343- input.mkv
For whatever reason, the resulting output file is not cutting on the IDR frames and the playback is funky. I have tried adjusting those frame ranges a few, never going above the IDR # (e.g., 56071, 56072, 56074, but not 56075+), and it doesn't make any difference.

Any ideas?

Last edited by Travillion; 20th January 2019 at 22:37.
Travillion is offline   Reply With Quote
Old 21st January 2019, 00:06   #2  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 1,611
So I can get this straight, you've got a video made of I-frames and other (dependent) frames:

Code:
I???????I???????I???????I???????I
And you are able to cut out an I-to-I section which plays back fine:

Code:
I???????I
We could say that's 56075-56195 (both I frames, if I read your post correctly).

But if you're cutting, don't you have to be sure that you are only discarding non-I-frames? Your "subtract 2 in order to be safe" doesn't seem consistent with that.

You should - again, if I've got this right, I might be wrong - only try to delete 56076-56194. If you delete any I-frames I think you'll ruin the GOPs either side, won't you?
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 21st January 2019, 01:26   #3  |  Link
Travillion
Registered User
 
Join Date: Aug 2018
Posts: 4
Quote:
Originally Posted by wonkey_monkey View Post
So I can get this straight, you've got a video made of I-frames and other (dependent) frames:

Code:
I???????I???????I???????I???????I
And you are able to cut out an I-to-I section which plays back fine:

Code:
I???????I
We could say that's 56075-56195 (both I frames, if I read your post correctly).

But if you're cutting, don't you have to be sure that you are only discarding non-I-frames? Your "subtract 2 in order to be safe" doesn't seem consistent with that.

You should - again, if I've got this right, I might be wrong - only try to delete 56076-56194. If you delete any I-frames I think you'll ruin the GOPs either side, won't you?
Yes, we're going for the same thing, but phrasing it a little differently. mkvmerge is supposed to cut on the I-frame at the beginning and right before the I-frame to end. So as you suggested: ...???[I?????]I???.... mkvmerge is supposed to do this intelligently (i.e., find the first I-frame at or after the designated frame) but it isn't always right.

There is something in my mkvmerge directions that is off, such that it will extract them properly but it's not discarding them properly. I can't figure out why. I'm sure my subtract 2 is not helping, but I've tried any number of variations around those cut points and still no success.
Travillion is offline   Reply With Quote
Old 21st January 2019, 03:40   #4  |  Link
hubblec4
Matroska find' ich toll
 
Join Date: Apr 2008
Posts: 958
Hey Travillion and welcome to Doom9

Mkvmerge and cutting works and sometimes not, this is depend from some things. I have many experience with this topic and the issue comes often by the audio.
Yes you can cut on video IDR frames but an audio frame is in Matroska not so easy to handle and often there are audio data which have a longer duration as the video.
Use MediaInfo for a quick overview about the durations. There is a global file duration and each video and audio track has a duration.
In MKVToolNix(MTX) info tool you get a better info about the durations.

After combine and/or cutting(with auto-merge) the file plays fine or not and some players can handle such files like MPC-HC(but not always).

If I understand you right you want cut out 3 small sections?!
56075-56195 : frame 56076 until 56194 should discard -> 118 frames
77926-77950 : -> 22 frames
80223-80343 : -> 118 frames

You could use Ordered Chapters instead of cutting. This will work perfect because you have IDR frames at the right place.
Chapter 1: Start time 00000 -> End time 56075
Chapter 2: Start time 56195 -> End time 77926
Chapter 3: Start time 77950 -> End time 80223
Chapter 4: Start time 80343 -> End time last frame(+frame duration)

The frames must be changed to a timestamp!
hubblec4 is offline   Reply With Quote
Old 21st January 2019, 04:08   #5  |  Link
Travillion
Registered User
 
Join Date: Aug 2018
Posts: 4
Quote:
Originally Posted by hubblec4 View Post
Hey Travillion and welcome to Doom9

Mkvmerge and cutting works and sometimes not, this is depend from some things. I have many experience with this topic and the issue comes often by the audio.
Yes you can cut on video IDR frames but an audio frame is in Matroska not so easy to handle and often there are audio data which have a longer duration as the video.
Use MediaInfo for a quick overview about the durations. There is a global file duration and each video and audio track has a duration.
In MKVToolNix(MTX) info tool you get a better info about the durations.

After combine and/or cutting(with auto-merge) the file plays fine or not and some players can handle such files like MPC-HC(but not always).

If I understand you right you want cut out 3 small sections?!
56075-56195 : frame 56076 until 56194 should discard -> 118 frames
77926-77950 : -> 22 frames
80223-80343 : -> 118 frames

You could use Ordered Chapters instead of cutting. This will work perfect because you have IDR frames at the right place.
Chapter 1: Start time 00000 -> End time 56075
Chapter 2: Start time 56195 -> End time 77926
Chapter 3: Start time 77950 -> End time 80223
Chapter 4: Start time 80343 -> End time last frame(+frame duration)

The frames must be changed to a timestamp!
Hmmm... interesting ideas Hubble4c. Your diagnosis and recommended solution make sense. I also wonder, though, if my source file is corrupt.

Since I seemed to be able to cut and join fine, just not discard, I decided to use that approach. I cut the file at each desired IDR break point and then appended the cuts, omitting the unwanted sections. This worked great for most of it, except I encountered an error with two of the files (not at join points).
"Warning: input.mkv: Error in the Matroska file structure at position xxxxx. Resyncing to the next level 1 element.
Warning: The last timestamp processed before the error was encountered was: xxxxxx.
Warning: Resyncing successful at position xxxxx.
Warning: The first cluster timestamp after the resync is xxxx."

As I said I got these for 2 of the 9 files, and they were not at join points. When I try and play the file, it plays fine up until the timestamp of the error, at which point audio completely cuts out and you can't seek to prior the error, and the movie duration is off.

Joining any of the other component files is fine. It's just these two files that bring up this error. I'm guessing the underlying files are corrupt somehow. Strangely, they play just fine by themselves, so I think it must be an error that is only brought to light when mkvmerge is trying to append (and thus timing info is messed with?).
Travillion is offline   Reply With Quote
Old 21st January 2019, 14:42   #6  |  Link
hubblec4
Matroska find' ich toll
 
Join Date: Apr 2008
Posts: 958
With Ordered Chapters you can so often and very fast change the timestamps to get the result like you need.
Cutting with mkvmerge and re-merge the cut-parts is a process which take to much time, and often you have to repeat it until the result is fine.
When I came to Matroska and found the feature "Ordered Chapters" I was so happy. You can also have multiple editions and one edition in my mkv's has complete/untouched chapters to play the whole content of the mkv.

With my chapterEditor is it an easy and fast task to generate multiple editions with different parts of the movie.
hubblec4 is offline   Reply With Quote
Old 21st January 2019, 16:11   #7  |  Link
Travillion
Registered User
 
Join Date: Aug 2018
Posts: 4
Quote:
Originally Posted by hubblec4 View Post
With Ordered Chapters you can so often and very fast change the timestamps to get the result like you need.
Cutting with mkvmerge and re-merge the cut-parts is a process which take to much time, and often you have to repeat it until the result is fine.
When I came to Matroska and found the feature "Ordered Chapters" I was so happy. You can also have multiple editions and one edition in my mkv's has complete/untouched chapters to play the whole content of the mkv.

With my chapterEditor is it an easy and fast task to generate multiple editions with different parts of the movie.
I have never used ordered chapters, but I like the idea more and more. I will check out your tool in your signature. Thanks for the ideas and assistance Hubblec4!
Travillion is offline   Reply With Quote
Old 22nd January 2019, 05:49   #8  |  Link
mkver
Registered User
 
Join Date: May 2016
Posts: 157
1. What FrameCounter tool are you talking about? I only found a tool of that name that appears to be made for showing the FPS of game consoles (the PSP). The reason I am asking this is to find out whether this tool is really able to distinguish IDR frames from ordinary keyframes; also, are its ranges in output order or in decoding order (and if the very first GOP contains undecodable leading frames (that are undecodable because they are missing references from the preceding GOP), how is this handled -- after all these frames lead to no output)?
2. In mkvmerge version 30.0 and 30.1, the files written are flushed after muxing is complete (from mkvmerge's point of view; at this stage, parts of the file are still in memory and not yet written to disc) in order to prevent errors like you have experienced (in future versions, the flushing will be an opt-in feature (see here)). So I am wondering what version of mkvmerge you're usng.
3. Probably the best you can do (apart from uploading the whole input file which probably isn't possible because it's copyrighted?) is to upload the output of "mkvinfo -s -r <output.log> <Matroska file>" for the relevant Matroska files somewhere.
4. The actual place for MKVToolNix bug reports is here.
mkver is offline   Reply With Quote
Reply

Tags
cut, idr, mkvmerge

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 00:11.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.