File: Convert_Comments.py

package info (click to toggle)
mediainfo 26.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,912 kB
  • sloc: cpp: 19,090; objc: 3,102; xml: 1,428; sh: 1,328; python: 263; makefile: 212
file content (63 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#   Type: Python 3 script
# Author: Steven Sun <stevenlele@outlook.com>
#   Date: Feb 27, 2020
#  Notes: Put this script in "MediaInfoLib/Source/Resource/Text/Stream" and run.
#         It reads "Audio,General,Image,Menu,Other,Text,Video.csv" and generates
#         "Data_Comments.csv" in Excel dialect.

import csv

filenames = ('Audio.csv', 'General.csv', 'Image.csv',
             'Menu.csv', 'Other.csv', 'Text.csv', 'Video.csv')

csv.register_dialect('MediaInfo', delimiter=';')

index_key = 0
index_type_unit = 2
index_comment = 6

# initiate CSV header
comment_rows = [['Key', 'Type or Unit', 'Comment']]
comment_dict = {}

for file in filenames:
    with open(file, 'r', encoding='utf_8') as f:
        reader = csv.reader(f, dialect='MediaInfo')

        for row in reader:
            row_key = row[index_key]
            row_type_unit = row[index_type_unit]
            row_comment = row[index_comment]

            if row_key.endswith('/String'):
                # merge Key and Key/String
                row_key = row_key[:-7]
            elif row_key[:-1].endswith('/String'):
                # ignore Key/String1, 2, 3,...
                continue
            elif '/' in row_key:
                row_key = row_key.split('/')[-1]

            if row_key in comment_dict:
                # if row_key exists, append information
                if row_type_unit and row_type_unit not in comment_dict[row_key][0]:
                    comment_dict[row_key][0].append(row_type_unit)
                if row_comment and row_comment not in comment_dict[row_key][1]:
                    comment_dict[row_key][1].append(row_comment)
            elif row_type_unit or row_comment:
                # if row_key does not exist, add to dictionary
                # format: {'key': ([type_or_unit_1, ...], [comment_1, ...]), ...}
                list_type_unit = []
                list_comment = []
                if row_type_unit:
                    list_type_unit.append(row_type_unit)
                if row_comment:
                    list_comment.append(row_comment)
                comment_dict[row_key] = (list_type_unit, list_comment)

for key, value in sorted(comment_dict.items()):
    comment_rows.append([key, '; '.join(value[0]), '; '.join(value[1])])

with open('Data_Comments.csv', 'w', encoding='utf_8_sig', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(comment_rows)