main.py 7.66 KB
Newer Older
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
"""MPAI CAE-ARP Packager.

Implements MPAI CAE-ARP Packager Technical Specification, providing:
- Access Copy Files:
  1. Restored Audio Files.
  2. Editing List.
  3. Set of Irregularity Images in a .zip file.
  4. Irregularity File.
- Preservation Master Files:
  1. Preservation Audio File.
  2. Preservation Audio-Visual File where the audio has been replaced with the Audio of the Preservation Audio File
     fully synchronised with the video.
  3. Set of Irregularity Images in a .zip file.
  4. Irregularity File.
"""

import json
import os
import shutil
import yaml
from moviepy.editor import VideoFileClip, AudioFileClip

__author__ = "Nadir Dalla Pozza"
__copyright__ = "Copyright 2022, Audio Innova S.r.l."
__credits__ = ["Niccolò Pretto", "Nadir Dalla Pozza", "Sergio Canazza"]
__license__ = "GPL"  # ?
__version__ = "1.0.1"
__maintainer__ = "Nadir Dalla Pozza"
__email__ = "nadir.dallapozza@unipd.it"
__status__ = "Production"


# Class for customizing console colors
class ConsoleColors:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARK_CYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'


if __name__ == '__main__':
    # Read configuration file
    config = object
    try:
        config = yaml.safe_load(open('./config.yaml', 'r'))
        if 'INPUT_PATH' not in config:
            print(ConsoleColors.RED + 'INPUT_PATH key not found in config.yaml!' + ConsoleColors.END)
            quit(os.EX_CONFIG)
        if 'OUTPUT_PATH' not in config:
            print(ConsoleColors.RED + 'OUTPUT_PATH key not found in config.yaml!' + ConsoleColors.END)
            quit(os.EX_CONFIG)
    except FileNotFoundError:
        print(ConsoleColors.RED + 'config.yaml file not found!' + ConsoleColors.END)
        quit(os.EX_NOINPUT)

    # Access Copy Files
    print('\n' + ConsoleColors.BOLD + 'Creation of Access Copy Files...' + ConsoleColors.END)

    # By default, AccessCopyFiles directory is created in the output path...
    acf_dir = 'AccessCopyFiles/'
    acf_path = os.path.join(config['OUTPUT_PATH'], acf_dir)
    # ...however, if it already exists, it is possible to skip its creation
    make_acf = False
    if not os.path.exists(acf_path):
        # Create directory
        os.mkdir(acf_path)
        make_acf = True
        print("Directory '% s' created" % acf_dir)
    else:
        print(ConsoleColors.PURPLE + 'AccessCopyFiles directory already exists!' + ConsoleColors.END)
        overwrite = input('Do you want to overwrite it? [y/n]: ')
        if overwrite.casefold() == 'y':
            # Overwrite directory
            shutil.rmtree(acf_path)
            os.mkdir(acf_path)
            make_acf = True
            print("Directory '% s' overwritten" % acf_dir)
        elif overwrite.casefold() != 'n':
            print(ConsoleColors.RED + 'Unknown command, exiting' + ConsoleColors.END)
            quit(os.EX_USAGE)

    if make_acf:
        # Copy RestoredAudioFiles
        raf_path = os.path.join(config['INPUT_PATH'], 'RestoredAudioFiles')
        if not os.path.exists(raf_path):
            print(ConsoleColors.RED + 'RestoredAudioFiles directory not found!' + ConsoleColors.END)
            quit(os.EX_NOINPUT)
        restored_audio_files = os.listdir(raf_path)
        if len(restored_audio_files) == 1:
            print(ConsoleColors.YELLOW + 'RestoredAudioFiles directory is empty' + ConsoleColors.END)
        shutil.copytree(raf_path, os.path.join(acf_path, 'RestoredAudioFiles'))
        print("Restored Audio Files copied")

        # Copy Editing List
        try:
            shutil.copy2(config['INPUT_PATH'] + 'EditingList.json', acf_path)
        except FileNotFoundError:
            print(ConsoleColors.RED + 'EditingList.json file not found!' + ConsoleColors.END)
            quit(os.EX_NOINPUT)
        print("Editing List copied")

        # Create Irregularity Images archive
        ii_path = os.path.join(config['INPUT_PATH'], 'IrregularityImages')
        if not os.path.exists(ii_path):
            print(ConsoleColors.RED + 'IrregularityImages directory not found!' + ConsoleColors.END)
            quit(os.EX_NOINPUT)
        irregularity_images = os.listdir(ii_path)
        if len(irregularity_images) == 1:
            print(ConsoleColors.YELLOW + 'IrregularityImages directory is empty' + ConsoleColors.END)
        shutil.make_archive(acf_path + 'IrregularityImages', 'zip', config['INPUT_PATH'], 'IrregularityImages')
        print("Irregularity Images archive created")

        # Copy Irregularity File
        try:
            shutil.copy2(config['INPUT_PATH'] + 'IrregularityFile.json', acf_path)
        except FileNotFoundError:
            print(ConsoleColors.RED + 'IrregularityFile.json file not found!' + ConsoleColors.END)
            quit(os.EX_NOINPUT)
        print("Irregularity File copied")

    # Preservation Master Files
    print('\n' + ConsoleColors.BOLD + 'Creation of Preservation Master Files...' + ConsoleColors.END)

    # By default, PreservationMasterFiles directory is created in the output path...
    pmf_dir = 'PreservationMasterFiles/'
    pmf_path = os.path.join(config['OUTPUT_PATH'], pmf_dir)
    # ...however, if it already exists, it is possible to skip its creation
    if not os.path.exists(pmf_path):
        os.mkdir(pmf_path)
        print("Directory '% s' created" % pmf_dir)
    else:
        print(ConsoleColors.PURPLE + 'PreservationMasterFiles directory already exists!' + ConsoleColors.END)
        overwrite = input('Do you want to overwrite it? [y/n]: ')
        if overwrite.casefold() == 'y':
            shutil.rmtree(pmf_path)
            os.mkdir(pmf_path)
            print("Directory '% s' overwritten" % pmf_dir)
        elif overwrite.casefold() == 'n':
            print('Exit\n')
            quit(os.EX_OK)
        else:
            print(ConsoleColors.RED + 'Unknown command, exiting' + ConsoleColors.END)
            quit(os.EX_USAGE)

    # Copy Preservation Audio File
    audio_file = 'PreservationAudioFile.wav'
    try:
        shutil.copy2(config['INPUT_PATH'] + audio_file, pmf_path)
    except FileNotFoundError:
        print(ConsoleColors.RED + audio_file + ' file not found!' + ConsoleColors.END)
        quit(os.EX_NOINPUT)
    print("Preservation Audio File copied")

    # Create Preservation Audio-Visual File with substituted audio
    video_file = 'PreservationAudioVisualFile.mov'
    try:
        video = VideoFileClip(config['INPUT_PATH'] + video_file)
        audio = AudioFileClip(config['INPUT_PATH'] + audio_file)
        # Open Irregularity File to get offset
        irregularity_file_json = open(config['INPUT_PATH'] + 'IrregularityFile.json')
        irregularity_file = json.load(irregularity_file_json)
        offset = irregularity_file['Offset']/1000
        if offset > 0:
            audio = audio.subclip(t_start=offset)
        else:
            video = video.subclip(t_start=offset)
        video = video.set_audio(audio)
        video.write_videofile(pmf_path + 'PreservationAudioVisualFile.mov', bitrate='3000k', codec='mpeg4')
        print("Preservation Audio-Visual File created")
    except OSError:
        print(ConsoleColors.RED + video_file + ' file not found!' + ConsoleColors.END)
        quit(os.EX_NOINPUT)

    # Create Irregularity Images archive (already checked)
    shutil.make_archive(pmf_path + 'IrregularityImages', 'zip', config['INPUT_PATH'], 'IrregularityImages')
    print("Irregularity Images archive created")

    # Copy Irregularity File (already checked)
    shutil.copy2(config['INPUT_PATH'] + 'IrregularityFile.json', pmf_path)
    print("Irregularity File copied")

    # End
    print('\n' + ConsoleColors.GREEN + ConsoleColors.BOLD + "Success!" + ConsoleColors.END + '\n')