File size: 2,505 Bytes
7630e84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
import os
import subprocess

from audio_to_transcript import TranscribeAudio
from translator import MyTranslator
from utils import log
from video_to_audio_converter import VideoToAudioConverter

with open('resources/languages.json', 'r') as f:
    code2lang = json.load(f)

# language code lookup by name, with a few language aliases
lang2code = {
    **{language: code for code, language in code2lang.items()},
}

LANGS = sorted(lang2code.keys())


class Pipeline:
    def __init__(self):
        self.video_to_audio = VideoToAudioConverter()
        self.audio_to_text = TranscribeAudio()
        self.translator = MyTranslator()

    def __call__(self, video_path: str, output_path: str, input_language: str, output_language: str):
        filename, ext = os.path.splitext(video_path)

        audio_path = self.video_to_audio.convert(video_path)
        subtitle_path = self.audio_to_text(audio_path, output_path, input_language)
        if input_language != output_language:
            subtitle_path = self.translator.translate(subtitle_path, lang2code[input_language],
                                                      lang2code[output_language])
        log(f"Embedding subtitles on input video and saves output video to {output_path}/output.mp4")
        # Use ffmpeg to add the subtitles to the input MP4 file and create the output MP4 file

        subtitles_cmd = ["ffmpeg", "-y", "-i", video_path, "-vf", f"subtitles={subtitle_path}", "-c:a", "copy",
                         f"{filename}_{output_language}_output.mp4"]

        subprocess.run(subtitles_cmd, check=True)
        return f"{filename}_{output_language}_output.mp4"


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("video", type=str,
                        help="video path to transcribe")
    parser.add_argument("--output_dir", "-o", type=str,
                        default=".", help="directory to save the outputs")
    parser.add_argument("--input_language", type=str, default=None, choices=LANGS,
                        help="language spoken in the video, skip to perform language detection")
    parser.add_argument("--output_language", type=str, default=None, choices=LANGS,
                        help="required translation language")

    args = parser.parse_args()
    pipeline = Pipeline()
    pipeline(args.video, args.output_dir, args.input_language, args.output_language)