sbraz/pymediainfo

[Question] How to display Menu's timestamps

codenotworking opened this issue · 2 comments

I'm trying to figure out how to display the timestamps in the Menu section of a video file. Here's my working basic code:

from pymediainfo import MediaInfo
media = MediaInfo.parse(r"c:\video.mkv")
for track in media.tracks:
    if track.track_type == "Menu":
        pprint(track.to_data())

This produces the following:

{'00_00_00000': 'en:00:00:00.000',
 '00_10_41683': 'en:00:10:41.683',
 '00_21_07308': 'en:00:21:07.308',
 '00_29_12876': 'en:00:29:12.876',
 '00_40_12744': 'en:00:40:12.744',
 '00_50_50464': 'en:00:50:50.464',
 '01_01_02033': 'en:01:01:02.033',
 '01_10_15420': 'en:01:10:15.420',
 '01_19_59086': 'en:01:19:59.086',
 '01_29_40709': 'en:01:29:40.709',
 '01_40_10338': 'en:01:40:10.338',
 '01_48_35801': 'en:01:48:35.801',
 '01_50_49977': 'en:01:50:49.977',
 'chapters_pos_begin': '94',
 'chapters_pos_end': '107',
 'count': '107',
 'count_of_stream_of_this_kind': '1',
 'kind_of_stream': 'Menu',
 'other_kind_of_stream': ['Menu'],
 'stream_identifier': '0',
 'track_type': 'Menu'}

I'm looking to extract only the timestamps from the produced dictionary, similar to how MediaInfo itself formats it:

0:00:00.000                              : en:00:00:00.000
00:10:41.683                             : en:00:10:41.683
00:21:07.308                             : en:00:21:07.308
00:29:12.876                             : en:00:29:12.876
00:40:12.744                             : en:00:40:12.744
00:50:50.464                             : en:00:50:50.464
01:01:02.033                             : en:01:01:02.033
01:10:15.420                             : en:01:10:15.420
01:19:59.086                             : en:01:19:59.086
01:29:40.709                             : en:01:29:40.709
01:40:10.338                             : en:01:40:10.338
01:48:35.801                             : en:01:48:35.801
01:50:49.977                             : en:01:50:49.977

I found a post on Sourceforge where someone asks the developer of MediaInfo for extracting the timestamps. The developer suggested some psuedocode, but I wouldn't even know where to begin:

Begin=MediaInfo::Get(Stream_Menu, x, "Chapters_Pos_Begin");
End=MediaInfo::Get(Stream_Menu, x, "Chapters_Pos_End");
for (i=Begin; i<End; ++i)
{
    Key=MediaInfo::Get(Stream_Menu, x, i, Info_Name);
    Value=MediaInfo::Get(Stream_Menu, x, i);
}

Any help on how to extract the Menu section's timestamps using python?

Never mind. I figured it out. I managed to create a simple for loop, using regex to find the timestamp dictionary keys to return the timestamp dictionary values, while also formatting the timestamp dictionary keys to look like real timestamps:

for key, value in track.to_data().items():
    if re.search(r"\d+_\d+_\d+", key):
        keyFormatted = key.replace("_", ":")
        keyFormatted = f"{keyFormatted[0:-3]}.{keyFormatted[-3:]}"
        print(f"{keyFormatted}\t{value}")

This produced:

00:00:00.000    en:00:00:00.000
00:10:41.683    en:00:10:41.683
00:21:07.308    en:00:21:07.308
00:29:12.876    en:00:29:12.876
00:40:12.744    en:00:40:12.744
00:50:50.464    en:00:50:50.464
01:01:02.033    en:01:01:02.033
01:10:15.420    en:01:10:15.420
01:19:59.086    en:01:19:59.086
01:29:40.709    en:01:29:40.709
01:40:10.338    en:01:40:10.338
01:48:35.801    en:01:48:35.801
01:50:49.977    en:01:50:49.977

This is a solution that can be used either way.

it renumbers the chapters in the correct format 01, 02, etc. However, it could be adjusted for use anyway.

https://gist.github.com/jlw4049/88b049951479eb4abd6822d6f2167a6f