Conversion_Kitchen_Code/kitchen_counter/conversion/subtitling/generate.py

107 lines
4.9 KiB
Python
Executable File

import sys
import os
from MNF.settings import BasePath
from .base import make_directory
from .google_srt import google_sub
from .AWS_S3 import aws_sub
from .awSRT import aws_final
from .azure_srt import azure_sub_temp
from .compare_srt_output import compare_subs
from MNF.settings import BasePath, MEDIA_ROOT
import moviepy.editor as mp
import random
# import logging
# basePath = BasePath()
# current = os.getcwd() + "/narration/Narration_Code"
# current = basePath + "/conversion/subtitling"
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# formatter = logging.Formatter(
# '%(asctime)s:%(levelname)s:%(lineno)d:%(name)s:%(message)s')
# file_handler = logging.FileHandler(rf'{current}/intro_collage_video.log')
# file_handler.setFormatter(formatter)
# logger.addHandler(file_handler)
# import google_srt
# import AWS_S3
# import awSRT
# import azure_srt
# import compare_srt_output
def generate_subtitle(mp4, lang_code):
# mp4 = "/home/user/mnf/project/MNF/media/videos/Subtitling_Videos/normal.mp4"
# lang = {
# 'Arabic': 'ar-AE',
# 'Bengali': 'bn-IN',
# 'Catalan': 'ca-ES',
# 'English': 'en-US',
# 'French': 'fr-FR',
# 'Gujarati': 'gr-IN',
# 'Hindi': 'hi-IN',
# 'Kannada': 'kn-IN',
# 'Marathi': 'mr-IN',
# 'Spanish': 'es-ES',
# 'Tamil': 'ta-IN',
# 'Telugu': 'te-IN'
# }
lang = {'Hindi': 'hi-IN', 'English': 'en-IN', 'Urdu': 'ur-PK', 'Arabic': 'ar-XA', 'Bengali': 'bn-IN', 'Kannada': 'kn-IN', 'Tamil': 'ta-IN', 'Bulgarian': 'bg-BG', 'Bangla': 'bn-BD',
'Malayalam': 'ml-IN', 'Russian': 'ml-IN', 'Serbian': 'sr-RS', 'Ukranian': 'uk-UA', 'Croatian': 'hr-HR', 'Irish': 'ga-IE', 'Albanian': 'sq',
'Marathi': 'mr-IN', 'Persian': 'fa-IR', 'Telugu': 'te-IN', 'Turkish': 'tr-TR', 'Hungarian': 'hu-HU', 'Italian': 'it-IT', 'Romanian': 'ro-RO', 'Punjabi': 'pa', 'Gujrati': 'gu-IN',
'Odia': 'or', 'Chinese': 'zh-CN', 'Nepali': 'ne', 'French': 'fr-FR', 'Spanish': 'es-ES', 'Indonesian': 'id-ID', 'Greek': 'el-GR', 'Japanese': 'ja-JP',
'Javanese': 'jv-ID', 'Korean': 'ko-KR', 'Belarusian': '', 'Uzbek': 'uz-UZ', 'Sindhi': '', 'Afrikaans': 'af-ZA', 'German': 'de-DE', 'Icelandic': '',
'Igbo': '', 'Latin': '', 'Portugese': 'pt-PT', 'Myanmar': 'my-MM', 'Thai': 'th-TH', 'Sundanese': '', 'Lao': 'lo', 'amharic': 'am-ET', 'Sinhala': '',
'Azerbaijani': 'az', 'Kazak': 'kk', 'Macedonian': '', 'Bosnian': 'bs', 'Pashto': 'ps', 'Malagasy': 'mg', 'Malay': 'ms', 'Yoruba': '',
'Czech': 'cs-CZ', 'Danish': 'da-DK', 'Dutch': 'nl-NL', 'Tagalog': '', 'Norwegian': 'nb-NO', 'Slovenian': 'sl-SI', ' Swedish': 'sv-SE', 'Vietnamese': 'vi-VN', 'Welsh': 'cy-GB',
'Hebrew': 'he', 'Armenian': 'hy', 'Khmer': 'km', 'Georgian': '', 'Mongolian': '', 'Kurdish': 'ku', 'Kyrgyz': '', 'Turkmen': '',
'Finnish': 'fi-FI', 'Haitian Creole': 'ht', 'Hawaiian': '', 'Lithuanian': 'lt-LT', 'Luxembourgish': '', 'Maltese': 'lt-LT', 'Polish': 'pl-PL',
'Esperanto': '', 'Tatar': '', 'Uyghur': '', 'Hausa': '', 'Somali': 'so-SO', 'Swahili': 'sw-KE', 'Yiddish': '', 'Basque': '', 'Catalan': 'ca-ES',
'Cebuano': '', 'Corsican': '', 'Estonian': 'et-EE', 'Frisian': '', 'Galician': 'gl-ES', 'Hmong': 'mww', 'Kinyarwanda': '', 'Latvian': '',
'Maori': 'mi', 'Samoan': 'sm', 'Scots Gaelic': '', 'Sesotho': '', 'Shona': '', 'Slovak': 'sk-SK', 'Xhosa': '', 'Zulu': 'zu-ZA'}
lang_code = lang[lang_code]
movie_name = os.path.splitext(mp4)[0]
# logger.debug(movie_name)
vid_path = make_directory(movie_name)
my_clip = mp.VideoFileClip(mp4) # uncomment when running from UI
# my_clip = mp.VideoFileClip(temp) # comment when running from command prompt
random_num = random.randint(0, 1000)
temp = vid_path + "/audio_" + str(random_num) + ".wav"
temp1 = "audio_" + str(random_num)+".wav"
print(temp1)
my_clip.audio.write_audiofile(temp)
print("Azure code is running")
try:
azure_sub_temp(temp1, lang_code, vid_path)
print("-"*30)
except:
pass
print("Google code is running")
try:
google_sub(temp1, lang_code, vid_path)
print("-"*30)
except:
pass
print("AWS code is running")
try:
aws_sub(temp1, lang_code, vid_path)
print("JSON created successfully")
# vid_path = "/home/user/mnf/project/MNF/media/videos/Subtitling_Videos/sample"
aws_final(vid_path)
print("-"*30)
except:
pass
print("Best SRT!")
return compare_subs(vid_path)
# mp4 = sys.argv[1] # comment when running from UI
# lang_code = sys.argv[2] # comment when running from UI
# generate_subtitle(mp4, lang_code) # comment when running from UI