File size: 10,899 Bytes
068ed60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
    This module defines the 'MKVProcessing' class, which processes MKV files.

    * Usage:
        To use this module, create an instance of the 'MKVProcessing' class and call the 'process_mkv' method.
    * Example: Frist create an instance of the MKVProcessing class:
        processor = MKVProcessing(filename="example.mkv",
                                crf_value="18",
                                preset_value="medium")
    * Example usage:
        processor.process_mkv(settings)
"""

import re
from contextlib import suppress
from dataclasses import dataclass
from os import listdir, path, remove, rename
from shutil import move
from subprocess import Popen, call
from typing import List, Dict, Callable, Optional

from constants import (WORKING_SPACE,
                       WORKING_SPACE_OUTPUT,
                       MKV_MERGE_PATH,
                       FFMPEG_PATH,
                       console)
from data.settings import Settings


@dataclass(slots=True)
class MKVProcessing:
    """
        The 'MKVProcessing' class is used for processing MKV files.

        Attributes:
            - filename (str): The name of the MKV file to process.
            - working_space (str): The path to the working directory.
            - working_space_output (str): The path to the output directory.
            - mkv_merge_path (str): The path to the MKVMerge executable.
            - ffmpeg_path (str): The path to the FFmpeg executable.
            - crf_value (str): The Constant Rate Factor value for FFmpeg.
            - preset_value (str): The preset value for FFmpeg.

        Methods:
            - process_mkv(self, settings: Settings) -> None:
                Processes the MKV file based on the specified settings.

            - move_files_to_working_space(self) -> None:
                Moves the processed files to the working directory.

            - mkv_merge(self) -> None:
                Merges the MKV file with the generated audio and subtitle files.

            - mkv_burn_to_mp4(self) -> None:
                Burns the subtitles into the MKV file and converts it to MP4.
    """
    filename: str
    working_space: str = WORKING_SPACE
    working_space_output: str = WORKING_SPACE_OUTPUT
    mkv_merge_path: str = MKV_MERGE_PATH
    ffmpeg_path: str = FFMPEG_PATH

    crf_value: str = '18'
    preset_value: str = 'ultrafast'

    def process_mkv(self, settings: Settings) -> None:
        """
            Processes the MKV file based on the specified settings.

            This method takes a Settings object as input and processes the MKV file accordingly. The processing options include moving the processed files to the working directory, merging the MKV file with the generated audio and subtitle files, and burning the subtitles into the MKV file and converting it to MP4.

            Args:
                settings (Settings): The settings for processing the MKV file.
        """
        options: Dict[str, Callable] = {
            'Oglądam w MM_AVH_Players (wynik: napisy i audio)': self.move_files_to_working_space,
            'Scal do mkv': self.mkv_merge,
            'Wypal do mp4': self.mkv_burn_to_mp4,
        }

        process_method: Optional[Callable] = options.get(settings.output)
        if process_method:
            console.print(
                f'\nRozpoczynam przetwarzane pliku o nazwie: {self.filename}...', style='green_bold')
            process_method()
            console.print(
                f'\nZakończono i zapisano plik o nazwie: {self.filename}... w odpowiednim folderze.', style='green_bold')

    def move_files_to_working_space(self) -> None:
        """
            Moves the processed files to the working directory.

            This method iterates over the files in the output directory and moves any files that start with the filename of the MKV file being processed to the working directory.
        """
        for filename in listdir(self.working_space_output):
            if filename.startswith(self.filename):
                move(path.join(self.working_space_output,
                     filename), self.working_space)

    def mkv_merge(self) -> None:
        """
            Merges the MKV file with the generated audio and subtitle files.

            This method takes the MKV file and merges it with the generated audio and subtitle files using the MKVMerge tool. The merged file is then saved in the output directory.
        """
        input_file: str = path.join(self.working_space, self.filename + '.mkv')
        if not path.exists(input_file):
            console.print(
                f'[red_bold]Plik {input_file} nie istnieje. Pomijam...')
            return
        output_file: str = path.join(
            self.working_space_output, self.filename + '.mkv')

        subtitle_file_srt: str = path.join(
            self.working_space_output, self.filename + '.srt')
        subtitle_file_ass: str = path.join(
            self.working_space_output, self.filename + '.ass')
        lector_file: str = path.join(
            self.working_space_output, self.filename + '.eac3')

        command: List[str] = [self.mkv_merge_path,
                              '-o', output_file, input_file]
        if path.exists(lector_file):
            command.extend(['--language', '0:pol', '--track-name',
                           '0:Lektor PL', '--default-track', '0:no', lector_file])
        if path.exists(subtitle_file_srt):
            command.extend(['--language', '0:pol', '--track-name',
                           '0:Napisy Poboczne PL', '--default-track', '0:no', subtitle_file_srt])
        elif path.exists(subtitle_file_ass):
            command.extend(['--language', '0:pol', '--track-name',
                           '0:Napisy Poboczne PL', '--default-track', '0:no', subtitle_file_ass])

        process = Popen(command)
        process.communicate()

        self._remove_files([subtitle_file_srt, subtitle_file_ass, lector_file])

    def mkv_burn_to_mp4(self) -> None:
        """
            Burns the subtitles into the MKV file and converts it to MP4.

            This method takes the MKV file, burns the subtitles into the video, and converts the file to MP4 format using FFmpeg. The converted file is then saved in the output directory.
        """
        filename: str = self.filename + '.mkv'
        if not path.exists(path.join(self.working_space, filename)):
            console.print(
                f'Plik {filename} nie istnieje w {self.working_space}. Pomijam...', style='red_bold')
            return
        new_filename: str = re.sub(r'[^A-Za-z0-9.]+', '_', filename)

        rename(path.join(self.working_space, filename),
               path.join(self.working_space, new_filename))

        subtitle_file_srt: str = path.join(
            self.working_space_output, self.filename + '.srt')
        subtitle_file_ass: str = path.join(
            self.working_space_output, self.filename + '.ass')
        lector_file: str = path.join(
            self.working_space_output, self.filename + '.eac3')

        output_file: str = path.join(
            self.working_space_output, new_filename[:-4] + '.mp4')

        command: List[str] = self._prepare_command(
            new_filename, output_file, lector_file, subtitle_file_srt, subtitle_file_ass)

        with suppress(Exception):
            call(command)

        if path.exists(output_file):
            target_file = path.join(
                self.working_space_output, filename[:-4] + '.mp4')
            if path.exists(target_file) and output_file != target_file:
                remove(target_file)
            rename(output_file, target_file)

        rename(path.join(self.working_space, new_filename),
               path.join(self.working_space, filename))

        self._remove_files([subtitle_file_srt, subtitle_file_ass, lector_file])

    def _remove_files(self, files: List[str]) -> None:
        """
            Removes the specified files.

            This method takes a list of file paths and removes each file if it exists.

            Args:
                files (List[str]): The list of files to remove.
        """
        for file in files:
            if path.exists(file):
                remove(file)

    def _prepare_command(self, new_filename: str, output_file: str, lector_file: str, subtitle_file_srt: str, subtitle_file_ass: str) -> List[str]:
        """
            Prepares the FFmpeg command for burning the subtitles and converting the file.

            This method takes the paths to the new filename, output file, lector file, and subtitle files, and prepares the FFmpeg command for burning the subtitles into the video and converting the file to MP4 format.

            Args:
                new_filename (str): The new filename for the MKV file.
                output_file (str): The path to the output file.
                lector_file (str): The path to the lector file.
                subtitle_file_srt (str): The path to the SRT subtitle file.
                subtitle_file_ass (str): The path to the ASS subtitle file.

            Returns:
                List[str]: The prepared FFmpeg command.
        """
        command: List[str] = []

        if (path.exists(subtitle_file_srt) or path.exists(subtitle_file_ass)) and path.exists(lector_file):
            command = [self.ffmpeg_path, '-y', '-i', path.join(self.working_space, new_filename), '-i', lector_file.replace("\\", "/")[
                2:], '-c:v', 'libx264', '-crf', self.crf_value, '-preset', self.preset_value, '-c:a', 'aac', '-map', '0:v:0', '-map', '1:a:0']

            if path.exists(subtitle_file_srt):
                command.extend(
                    ['-vf', 'subtitles=' + subtitle_file_srt.replace("\\", "/")[2:]])
            elif path.exists(subtitle_file_ass):
                command.extend(
                    ['-vf', 'subtitles=' + subtitle_file_ass.replace("\\", "/")[2:]])

        elif path.exists(lector_file):
            command = [self.ffmpeg_path, '-y', '-i', path.join(self.working_space, new_filename), '-i', lector_file.replace(
                "\\", "/")[2:], '-c:v', 'copy', '-c:a', 'aac', '-map', '0:v:0', '-map', '1:a:0']

        elif path.exists(subtitle_file_srt) or path.exists(subtitle_file_ass):
            command = [self.ffmpeg_path, '-y', '-i', path.join(self.working_space, new_filename),
                       '-c:v', 'libx264', '-crf', self.crf_value, '-preset', self.preset_value, '-c:a', 'copy']

            if path.exists(subtitle_file_srt):
                command.extend(
                    ['-vf', 'subtitles=' + subtitle_file_srt.replace("\\", "/")[2:]])
            elif path.exists(subtitle_file_ass):
                command.extend(
                    ['-vf', 'subtitles=' + subtitle_file_ass.replace("\\", "/")[2:]])

        command.append(output_file.replace("\\", "/")[2:])

        return command