Log in

View Full Version : convert list of frame numbers into mkv chapters


Hobojobo
27th October 2008, 14:10
Is there a software, how I can concert celltimes into a complaint mkv chapter list:

9687
21250
30449
35500

to

CHAPTER01=00:00:00.000
CHAPTER01NAME=Intro
CHAPTER02=00:06:27.480
CHAPTER02NAME=Act 1
CHAPTER03=00:14:10.000
CHAPTER03NAME=Act 2
CHAPTER04=00:20:17.960
CHAPTER04NAME=Act 3

etc.

As easy as possible, I mean. ;-)

mkvmerge does not import a plain frame list, does it?

TheFluff
27th October 2008, 22:18
No, mkvmerge does not support that, but it would be fairly trivial to implement a program or script that could do such a conversion in one of two ways: either by assuming a fixed FPS (dangerous) or by reading a v2 timecodes file (as output by mkvextract) and mapping frame numbers to timestamps.

For the second way, in Perl you could do something like this:
use warnings;
use strict;

my ($framesfile, $tcfile, $chaptersfile) = @ARGV;

open(FRAMES, "<", $framesfile) or die $!;
open(TCS, "<", $tcfile) or die $!;
open(CHAPTERS, ">", $chaptersfile) or die $!;

my (@frames, @tcs, @chapters);
my $chapter = 1;

die("$tcfile is not a v2 tc file") unless (readline(TCS) =~ /# timecode format v2/i);

chomp(@frames = <FRAMES>);
chomp(@tcs = <TCS>);

foreach (@frames) {
die("frame number $_ out of range") if ($_ > $#tcs);
print CHAPTERS makechapter($tcs[$_], $chapter);
$chapter++;
}

sub makechapter {
my ($ts, $chpno) = @_;
my ($hr, $min, $s, $ms) = (0,0,0,0);

while ($ts >= 3_600_000) {
$ts -= 3_600_000; $hr++;
}
while ($ts >= 60_000) {
$ts -= 60_000; $min++;
}
while ($ts >= 1_000) {
$ts -= 1_000; $s++;
}
$ms = $ts;

return(sprintf("CHAPTER%02d=%02d:%02d:%02d.%03d\nCHAPTER%02dNAME=Chapter %d\n",
$chpno, $hr, $min, $s, $ms, $chpno, $chpno));
}
which would take one text file with one frame number per line (first frame is number 0, like in avisynth), one v2 timecodes file and one output file on the commandline and generate chapters based on the frame numbers.

Hobojobo
28th October 2008, 07:51
Thanks for the script.
:)

One more question:

My videos all have 25 fps.
So I do not need a v2 timecodes file.

Which parts do I have to change in the script, when the FPS is known?

TheFluff
28th October 2008, 16:11
Thanks for the script.
:)

One more question:

My videos all have 25 fps.
So I do not need a v2 timecodes file.

Which parts do I have to change in the script, when the FPS is known?

use warnings;
use strict;

my ($framesfile, $fps, $chaptersfile) = @ARGV;

open(FRAMES, "<", $framesfile) or die $!;
open(CHAPTERS, ">", $chaptersfile) or die $!;

die("invalid fps") unless ($fps =~ /^\d+(\.\d+)?/);

my (@frames, @chapters);
my $chapter = 1;
my $framelen = 1000/$fps;

chomp(@frames = <FRAMES>);
select(CHAPTERS);

foreach (@frames) {
print CHAPTERS makechapter($framelen*$_, $chapter);
$chapter++;
}

sub makechapter {
my ($ts, $chpno) = @_;
my ($hr, $min, $s, $ms) = (0,0,0,0);

while ($ts >= 3_600_000) {
$ts -= 3_600_000; $hr++;
}
while ($ts >= 60_000) {
$ts -= 60_000; $min++;
}
while ($ts >= 1_000) {
$ts -= 1_000; $s++;
}
$ms = $ts;

return(sprintf("CHAPTER%02d=%02d:%02d:%02d.%03d\nCHAPTER%02dNAME=Chapter %d\n",
$chpno, $hr, $min, $s, $ms, $chpno, $chpno));
}

takes a fixed fps value instead of a v2 timecodes file
not tested

Hobojobo
28th October 2008, 20:02
I'm really thankful for your effort.

But I can't even get the script running.
It's a perl script, right.


So I have to get/install a perl interpreter for windows.
I have to name the script something.pl.

I think, I am too stupid.
I keep trying.

:stupid:

TheFluff
28th October 2008, 20:07
1) go to http://www.activestate.com/Products/activeperl/index.mhtml and click "activeperl, download now" (you don't need to subscribe to their newsletter or anything like that)
2) install it
3) copypaste the code I pasted above into a text file (as in, use notepad) and save it as whateveryouwant.pl
4) go start -> run -> type cmd and hit enter
5) in the box that pops up type
perl "X:/folder/whateveryouwant.pl" "y:/some other folder/framenumbersfile.txt" 25 "z:/some other folder/chaptersfile.txt"
and hit enter
(25 is the fps of the file)

Hobojobo
28th October 2008, 20:08
I'm really thankful for your effort.

But I can't even get the script running.
It's a perl script, right.


So I have to get/install a perl interpreter for windows.
I have to name the script something.pl.

How do I call/run the script?
something.pl frame_list.txt,25,chapters.txt ?
Does not work.

I think, I am too stupid.
I keep trying.

:stupid:

TheFluff
28th October 2008, 21:21
I just posted exactly what you need to do, if you cannot follow those simple instructions then I guess you better find some other program

NanoBot
28th October 2008, 21:53
Hi,

this little program might be helpful to convert framenumbers to timecodes and others:

http://www.mediachance.com/dvdlab/freetime.htm

The only disadvantage I see is, that this calculator only uses two digits for the milliseconds fraction, while matroska chapter files need three digits.

C.U. NanoBot

Hobojobo
29th October 2008, 18:20
@TheFluff

Thank you again.
I did it. And it works great.

My last post was a double post and was sent by mistake, it wasn't a response to your instruction. Sorry. :o

Thanks again for your quick helpful answers.