forked from tagunzet/Translate_Youtube_movie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgts.py
133 lines (85 loc) · 3.19 KB
/
gts.py
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
import os ## Требуется для os.path и поиска пути директории
import youtube_dl # импортирую библиотеку
import time
import ffmpeg
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
with ydl:
result = ydl.extract_info( ##извлекаем информацию о видеофайле из ссылки
'https://www.youtube.com/watch?v=hVivvHS4QZQ', ## ссылка на видеофайл
download=False ## Скачать информацию True False
)
if 'entries' in result:
# Если плейлист или список
video = result['entries'][0]
else:
## просто видео
video = result
slovar = video.keys()
videos = video['formats'][6]['url']
print(video['formats'][6]['url'])
audios = video['formats'][3]['url']
print(video['formats'][3]['url'])
import random
import wget
namefile = 'hehes'+ str(random.randint(0,999))
print(namefile)
wget.download(videos, namefile+".mpeg")
#wget.download(audios, "yt_sound.mp3")
path_vid = os.path.join(os.getcwd(), 'yt_vid.mpeg') ## Путь до видеофайла
path_audio = os.path.join(os.getcwd(), 'yt_sound.mp3') ## Путь до аудиофайла
import youtube_dl
ydl_opts = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True, # only keep the audio
'audioformat' : "mp3", # convert to mp3
'outtmpl': f"{os.path.join(os.getcwd(), namefile)}.mp3", #name
'noplaylist' : True, # download single, not playlist
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=hVivvHS4QZQ'])
start_time = time.time()
import speech_recognition as sr
from os import path
from pydub import AudioSegment
AudioSegment.converter = "ffmpeg.exe"
AudioSegment.ffmpeg = "ffmpeg.exe"
AudioSegment.ffprobe ="ffprobe.exe"
# convert mp3 file to wav
sound = AudioSegment.from_file(namefile+".mp3")
#first_20_seconds = sound[:20000]
beginning = sound
beginning.export("outputfile.wav", format="wav")
from speech_recognition import *
import speech_recognition
sample_audio = speech_recognition.AudioFile('outputfile.wav')
r = sr.Recognizer()
with sr.AudioFile('outputfile.wav') as source:
audio = r.record(source) # read the entire audio file
print(type(audio))
massslov = []
try:
# Just pass a language parameter
out = r.recognize_sphinx(audio, language="en-US")
massslov.append(out)
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
print("--- %s seconds ---" % (time.time() - start_time))
print(massslov)
from deep_translator import GoogleTranslator
to_translate = massslov[0]
translated = GoogleTranslator(source='auto', target='ru').translate(to_translate)
print(translated)
from gtts import gTTS
import os
text = translated
language = 'ru'
speech = gTTS(text = text, lang = language, slow = False)
speech.save("text.mp3")
#os.system("start text.mp3")
from mhmovie import *
m = Movie(namefile+".mpeg")
mu = Music('text.mp3')
final = m+mu
final.save(namefile+"film.mp4")