EC2 Default User commited on
Commit
82fb340
1 Parent(s): 7ccf97f

Fixing order of importation of modules

Browse files
Files changed (2) hide show
  1. app.py +241 -1
  2. utils/modules.py +0 -242
app.py CHANGED
@@ -12,7 +12,247 @@ VOICE_PATH = "utils/"
12
  # add libraries into environment
13
  sys.path.append(VOICE_PATH) # set this if modules and voice are not installed globally
14
  from utils.voice import *
15
- from utils.modules import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  #Definition Web App in Gradio
17
  text_to_say=gr.inputs.Textbox(label='What would you like the voice to say? (max. 2000 characters per request)')
18
  url =gr.inputs.Textbox(label = "Enter the YouTube URL below:")
 
12
  # add libraries into environment
13
  sys.path.append(VOICE_PATH) # set this if modules and voice are not installed globally
14
  from utils.voice import *
15
+ # Modules for the Video Messsage Generator From Youtube
16
+ from IPython.display import HTML, Audio
17
+ from base64 import b64decode
18
+ import numpy as np
19
+ from scipy.io.wavfile import read as wav_read
20
+ import io
21
+ import ffmpeg
22
+ from pytube import YouTube
23
+ import random
24
+ from subprocess import call
25
+ from datetime import datetime
26
+
27
+ def time_between(t1, t2):
28
+ FMT = '%H:%M:%S'
29
+ t1 = datetime.strptime(t1, FMT)
30
+ t2 = datetime.strptime(t2, FMT)
31
+ delta = t2 - t1
32
+ return str(delta)
33
+
34
+ def download_video(url):
35
+
36
+ print("Downloading...")
37
+ local_file = (
38
+ YouTube(url)
39
+ .streams.filter(progressive=True, file_extension="mp4")
40
+ .first()
41
+ .download(filename="youtube{}.mp4".format(random.randint(0, 10000)))
42
+ )
43
+ print("Downloaded")
44
+ return local_file
45
+ # download(output_path=destination, filename="name.mp4")
46
+
47
+
48
+ def download_youtube(url):
49
+ #Select a Youtube Video
50
+ #find youtube video id
51
+ from urllib import parse as urlparse
52
+ url_data = urlparse.urlparse(url)
53
+ query = urlparse.parse_qs(url_data.query)
54
+ YOUTUBE_ID = query["v"][0]
55
+ url_download ="https://www.youtube.com/watch?v={}".format(YOUTUBE_ID)
56
+ # download the youtube with the given ID
57
+ os.system("{} youtube-dl -f mp4 --output youtube.mp4 '{}'".format(env,url_download))
58
+ return "youtube.mp4"
59
+
60
+
61
+
62
+ def cleanup():
63
+ import pathlib
64
+ import glob
65
+ types = ('*.mp4','*.mp3', '*.wav') # the tuple of file types
66
+ #Finding mp4 and wave files
67
+ junks = []
68
+ for files in types:
69
+ junks.extend(glob.glob(files))
70
+ try:
71
+ # Deleting those files
72
+ for junk in junks:
73
+ print("Deleting",junk)
74
+ # Setting the path for the file to delete
75
+ file = pathlib.Path(junk)
76
+ # Calling the unlink method on the path
77
+ file.unlink()
78
+ except Exception:
79
+ print("I cannot delete the file because it is being used by another process")
80
+
81
+
82
+ def clean_data():
83
+ # importing all necessary libraries
84
+ import sys, os
85
+ # initial directory
86
+ home_dir = os.getcwd()
87
+ # some non existing directory
88
+ fd = 'sample_data/'
89
+ # Join various path components
90
+ path_to_clean=os.path.join(home_dir,fd)
91
+ print("Path to clean:",path_to_clean)
92
+ # trying to insert to false directory
93
+ try:
94
+ os.chdir(path_to_clean)
95
+ print("Inside to clean", os.getcwd())
96
+ cleanup()
97
+ # Caching the exception
98
+ except:
99
+ print("Something wrong with specified\
100
+ directory. Exception- ", sys.exc_info())
101
+ # handling with finally
102
+ finally:
103
+ print("Restoring the path")
104
+ os.chdir(home_dir)
105
+ print("Current directory is-", os.getcwd())
106
+
107
+ def youtube_trim(url,start,end):
108
+ #cancel previous youtube
109
+ cleanup()
110
+ #download youtube
111
+ #download_youtube(url) # with youtube-dl (slow)
112
+ input_videos=download_video(url)
113
+ # Get the current working directory
114
+ parent_dir = os.getcwd()
115
+ # Trim the video (start, end) seconds
116
+ start = start
117
+ end = end
118
+ #Note: the trimmed video must have face on all frames
119
+ #interval = end - start
120
+ interval = time_between(start, end)
121
+ #trimmed_video= parent_dir+'/sample_data/input_vid{}.mp4'.format(random.randint(0, 10000))
122
+ #trimmed_audio= parent_dir+'/sample_data/input_audio{}.mp3'.format(random.randint(0, 10000))
123
+ trimmed_video= parent_dir+'/sample_data/input_video.mp4'
124
+ trimmed_audio= parent_dir+'/sample_data/input_audio.mp3'
125
+ #delete trimmed if already exits
126
+ clean_data()
127
+ #call(["rm","-f",trimmed_audio])
128
+ #call(["rm","-f",trimmed_video])
129
+
130
+ #!rm -f {trimmed_video}
131
+ # cut the video
132
+ call(["ffmpeg","-y","-i",input_videos,"-ss", start,"-t",interval,"-async","1",trimmed_video])
133
+ #!ffmpeg -y -i youtube.mp4 -ss {start} -t {interval} -async 1 {trimmed_video}
134
+ # cut the audio
135
+ call(["ffmpeg","-i",trimmed_video, "-q:a", "0", "-map","a",trimmed_audio])
136
+ #Preview trimmed video
137
+ #clear_output()
138
+ print("Trimmed Video+Audio")
139
+ return trimmed_video, trimmed_audio
140
+
141
+ def create_video(Text,Voicetoclone):
142
+ out_audio=greet(Text,Voicetoclone)
143
+ current_dir=os.getcwd()
144
+ clonned_audio = os.path.join(current_dir, out_audio)
145
+
146
+ #Start Crunching and Preview Output
147
+ #Note: Only change these, if you have to
148
+ pad_top = 0#@param {type:"integer"}
149
+ pad_bottom = 10#@param {type:"integer"}
150
+ pad_left = 0#@param {type:"integer"}
151
+ pad_right = 0#@param {type:"integer"}
152
+ rescaleFactor = 1#@param {type:"integer"}
153
+ nosmooth = False #@param {type:"boolean"}
154
+
155
+ out_name ="result_voice_{}.mp4".format(random.randint(0, 10000))
156
+ out_file="../"+out_name
157
+
158
+ if nosmooth == False:
159
+ os.system('{} cd Wav2Lip && python inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face "../sample_data/input_video.mp4" --audio "../out/clonned_audio.wav" --outfile {} --pads {} {} {} {} --resize_factor {}'.format(env,out_file,pad_top ,pad_bottom ,pad_left ,pad_right ,rescaleFactor))
160
+ else:
161
+ os.system('{} cd Wav2Lip && python inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face "../sample_data/input_video.mp4" --audio "../out/clonned_audio.wav" --outfile {} --pads {} {} {} {} --resize_factor {} --nosmooth'.format(env,out_file,pad_top ,pad_bottom ,pad_left ,pad_right ,rescaleFactor))
162
+
163
+ #clear_output()
164
+ print("Creation of Video done")
165
+ return out_name
166
+
167
+
168
+ def time_format_check(input1):
169
+ timeformat = "%H:%M:%S"
170
+ #input1 = input("At what time did sensor 1 actuate? ")
171
+ try:
172
+ validtime = datetime.strptime(input1, timeformat)
173
+ print("The time format is valid", input1)
174
+ #Do your logic with validtime, which is a valid format
175
+ return False
176
+ except ValueError:
177
+ print("The time {} has not valid format hh:mm:ss".format(input1))
178
+ return True
179
+
180
+
181
+ def to_seconds(datetime_obj):
182
+ from datetime import datetime
183
+ time =datetime_obj
184
+ date_time = datetime.strptime(time, "%H:%M:%S")
185
+ a_timedelta = date_time - datetime(1900, 1, 1)
186
+ seconds = a_timedelta.total_seconds()
187
+ return seconds
188
+
189
+
190
+ def validate_youtube(url):
191
+ #This creates a youtube objet
192
+ try:
193
+ yt = YouTube(url)
194
+ except Exception:
195
+ print("Hi there URL seems invalid")
196
+ return True, 0
197
+ #This will return the length of the video in sec as an int
198
+ video_length = yt.length
199
+ if video_length > 600:
200
+ print("Your video is larger than 10 minutes")
201
+ return True, video_length
202
+ else:
203
+ print("Your video is less than 10 minutes")
204
+ return False, video_length
205
+
206
+
207
+ def video_generator(text_to_say,url,initial_time,final_time):
208
+ print('Checking the url',url)
209
+ check1, video_length = validate_youtube(url)
210
+ if check1 is True: return "./demo/tryagain2.mp4"
211
+ check2 = validate_time(initial_time,final_time, video_length)
212
+ if check2 is True: return "./demo/tryagain0.mp4"
213
+ trimmed_video, trimmed_audio=youtube_trim(url,initial_time,final_time)
214
+ voicetoclone=trimmed_audio
215
+ print(voicetoclone)
216
+ outvideo=create_video(text_to_say,voicetoclone)
217
+ #Preview output video
218
+ print("Final Video Preview")
219
+ final_video= parent_dir+'/'+outvideo
220
+ print("DONE")
221
+ #showVideo(final_video)
222
+ return final_video
223
+
224
+
225
+ def validate_time(initial_time,final_time,video_length):
226
+ is_wrong1=time_format_check(initial_time)
227
+ is_wrong2=time_format_check(final_time)
228
+ #print(is_wrong1,is_wrong2)
229
+ if is_wrong1 is False and is_wrong2 is False:
230
+ delta=time_between(initial_time,final_time)
231
+ if len(str(delta)) > 8:
232
+ print("Final Time is Smaller than Initial Time: t1>t2")
233
+ is_wrong = True
234
+ return is_wrong
235
+ else:
236
+ print("OK")
237
+ is_wrong=False
238
+ if int(to_seconds(delta)) > 300 :
239
+ print("The trim is larger than 5 minutes")
240
+ is_wrong = True
241
+ return is_wrong
242
+
243
+ elif int(to_seconds(delta)) > video_length :
244
+ print("The trim is larger than video lenght")
245
+ is_wrong = True
246
+ return is_wrong
247
+ else:
248
+ return is_wrong
249
+
250
+ else:
251
+ print("Your time format is invalid")
252
+ is_wrong = True
253
+ return is_wrong
254
+
255
+
256
  #Definition Web App in Gradio
257
  text_to_say=gr.inputs.Textbox(label='What would you like the voice to say? (max. 2000 characters per request)')
258
  url =gr.inputs.Textbox(label = "Enter the YouTube URL below:")
utils/modules.py DELETED
@@ -1,242 +0,0 @@
1
- # Modules for the Video Messsage Generator From Youtube
2
-
3
- from IPython.display import HTML, Audio
4
- from base64 import b64decode
5
- import numpy as np
6
- from scipy.io.wavfile import read as wav_read
7
- import io
8
- import ffmpeg
9
- from pytube import YouTube
10
- import random
11
- from subprocess import call
12
- import os
13
- from datetime import datetime
14
-
15
-
16
- def time_between(t1, t2):
17
- FMT = '%H:%M:%S'
18
- t1 = datetime.strptime(t1, FMT)
19
- t2 = datetime.strptime(t2, FMT)
20
- delta = t2 - t1
21
- return str(delta)
22
-
23
- def download_video(url):
24
-
25
- print("Downloading...")
26
- local_file = (
27
- YouTube(url)
28
- .streams.filter(progressive=True, file_extension="mp4")
29
- .first()
30
- .download(filename="youtube{}.mp4".format(random.randint(0, 10000)))
31
- )
32
- print("Downloaded")
33
- return local_file
34
- # download(output_path=destination, filename="name.mp4")
35
-
36
-
37
- def download_youtube(url):
38
- #Select a Youtube Video
39
- #find youtube video id
40
- from urllib import parse as urlparse
41
- url_data = urlparse.urlparse(url)
42
- query = urlparse.parse_qs(url_data.query)
43
- YOUTUBE_ID = query["v"][0]
44
- url_download ="https://www.youtube.com/watch?v={}".format(YOUTUBE_ID)
45
- # download the youtube with the given ID
46
- os.system("{} youtube-dl -f mp4 --output youtube.mp4 '{}'".format(env,url_download))
47
- return "youtube.mp4"
48
-
49
-
50
-
51
- def cleanup():
52
- import pathlib
53
- import glob
54
- types = ('*.mp4','*.mp3', '*.wav') # the tuple of file types
55
- #Finding mp4 and wave files
56
- junks = []
57
- for files in types:
58
- junks.extend(glob.glob(files))
59
- try:
60
- # Deleting those files
61
- for junk in junks:
62
- print("Deleting",junk)
63
- # Setting the path for the file to delete
64
- file = pathlib.Path(junk)
65
- # Calling the unlink method on the path
66
- file.unlink()
67
- except Exception:
68
- print("I cannot delete the file because it is being used by another process")
69
-
70
-
71
- def clean_data():
72
- # importing all necessary libraries
73
- import sys, os
74
- # initial directory
75
- home_dir = os.getcwd()
76
- # some non existing directory
77
- fd = 'sample_data/'
78
- # Join various path components
79
- path_to_clean=os.path.join(home_dir,fd)
80
- print("Path to clean:",path_to_clean)
81
- # trying to insert to false directory
82
- try:
83
- os.chdir(path_to_clean)
84
- print("Inside to clean", os.getcwd())
85
- cleanup()
86
- # Caching the exception
87
- except:
88
- print("Something wrong with specified\
89
- directory. Exception- ", sys.exc_info())
90
- # handling with finally
91
- finally:
92
- print("Restoring the path")
93
- os.chdir(home_dir)
94
- print("Current directory is-", os.getcwd())
95
-
96
- def youtube_trim(url,start,end):
97
- #cancel previous youtube
98
- cleanup()
99
- #download youtube
100
- #download_youtube(url) # with youtube-dl (slow)
101
- input_videos=download_video(url)
102
- # Get the current working directory
103
- parent_dir = os.getcwd()
104
- # Trim the video (start, end) seconds
105
- start = start
106
- end = end
107
- #Note: the trimmed video must have face on all frames
108
- #interval = end - start
109
- interval = time_between(start, end)
110
- #trimmed_video= parent_dir+'/sample_data/input_vid{}.mp4'.format(random.randint(0, 10000))
111
- #trimmed_audio= parent_dir+'/sample_data/input_audio{}.mp3'.format(random.randint(0, 10000))
112
- trimmed_video= parent_dir+'/sample_data/input_video.mp4'
113
- trimmed_audio= parent_dir+'/sample_data/input_audio.mp3'
114
- #delete trimmed if already exits
115
- clean_data()
116
- #call(["rm","-f",trimmed_audio])
117
- #call(["rm","-f",trimmed_video])
118
-
119
- #!rm -f {trimmed_video}
120
- # cut the video
121
- call(["ffmpeg","-y","-i",input_videos,"-ss", start,"-t",interval,"-async","1",trimmed_video])
122
- #!ffmpeg -y -i youtube.mp4 -ss {start} -t {interval} -async 1 {trimmed_video}
123
- # cut the audio
124
- call(["ffmpeg","-i",trimmed_video, "-q:a", "0", "-map","a",trimmed_audio])
125
- #Preview trimmed video
126
- #clear_output()
127
- print("Trimmed Video+Audio")
128
- return trimmed_video, trimmed_audio
129
-
130
- def create_video(Text,Voicetoclone):
131
- out_audio=greet(Text,Voicetoclone)
132
- current_dir=os.getcwd()
133
- clonned_audio = os.path.join(current_dir, out_audio)
134
-
135
- #Start Crunching and Preview Output
136
- #Note: Only change these, if you have to
137
- pad_top = 0#@param {type:"integer"}
138
- pad_bottom = 10#@param {type:"integer"}
139
- pad_left = 0#@param {type:"integer"}
140
- pad_right = 0#@param {type:"integer"}
141
- rescaleFactor = 1#@param {type:"integer"}
142
- nosmooth = False #@param {type:"boolean"}
143
-
144
- out_name ="result_voice_{}.mp4".format(random.randint(0, 10000))
145
- out_file="../"+out_name
146
-
147
- if nosmooth == False:
148
- os.system('{} cd Wav2Lip && python inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face "../sample_data/input_video.mp4" --audio "../out/clonned_audio.wav" --outfile {} --pads {} {} {} {} --resize_factor {}'.format(env,out_file,pad_top ,pad_bottom ,pad_left ,pad_right ,rescaleFactor))
149
- else:
150
- os.system('{} cd Wav2Lip && python inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face "../sample_data/input_video.mp4" --audio "../out/clonned_audio.wav" --outfile {} --pads {} {} {} {} --resize_factor {} --nosmooth'.format(env,out_file,pad_top ,pad_bottom ,pad_left ,pad_right ,rescaleFactor))
151
-
152
- #clear_output()
153
- print("Creation of Video done")
154
- return out_name
155
-
156
-
157
- def time_format_check(input1):
158
- timeformat = "%H:%M:%S"
159
- #input1 = input("At what time did sensor 1 actuate? ")
160
- try:
161
- validtime = datetime.strptime(input1, timeformat)
162
- print("The time format is valid", input1)
163
- #Do your logic with validtime, which is a valid format
164
- return False
165
- except ValueError:
166
- print("The time {} has not valid format hh:mm:ss".format(input1))
167
- return True
168
-
169
-
170
- def to_seconds(datetime_obj):
171
- from datetime import datetime
172
- time =datetime_obj
173
- date_time = datetime.strptime(time, "%H:%M:%S")
174
- a_timedelta = date_time - datetime(1900, 1, 1)
175
- seconds = a_timedelta.total_seconds()
176
- return seconds
177
-
178
-
179
- def validate_youtube(url):
180
- #This creates a youtube objet
181
- try:
182
- yt = YouTube(url)
183
- except Exception:
184
- print("Hi there URL seems invalid")
185
- return True, 0
186
- #This will return the length of the video in sec as an int
187
- video_length = yt.length
188
- if video_length > 600:
189
- print("Your video is larger than 10 minutes")
190
- return True, video_length
191
- else:
192
- print("Your video is less than 10 minutes")
193
- return False, video_length
194
-
195
-
196
- def video_generator(text_to_say,url,initial_time,final_time):
197
- print('Checking the url',url)
198
- check1, video_length = validate_youtube(url)
199
- if check1 is True: return "./demo/tryagain2.mp4"
200
- check2 = validate_time(initial_time,final_time, video_length)
201
- if check2 is True: return "./demo/tryagain0.mp4"
202
- trimmed_video, trimmed_audio=youtube_trim(url,initial_time,final_time)
203
- voicetoclone=trimmed_audio
204
- print(voicetoclone)
205
- outvideo=create_video(text_to_say,voicetoclone)
206
- #Preview output video
207
- print("Final Video Preview")
208
- final_video= parent_dir+'/'+outvideo
209
- print("DONE")
210
- #showVideo(final_video)
211
- return final_video
212
-
213
-
214
- def validate_time(initial_time,final_time,video_length):
215
- is_wrong1=time_format_check(initial_time)
216
- is_wrong2=time_format_check(final_time)
217
- #print(is_wrong1,is_wrong2)
218
- if is_wrong1 is False and is_wrong2 is False:
219
- delta=time_between(initial_time,final_time)
220
- if len(str(delta)) > 8:
221
- print("Final Time is Smaller than Initial Time: t1>t2")
222
- is_wrong = True
223
- return is_wrong
224
- else:
225
- print("OK")
226
- is_wrong=False
227
- if int(to_seconds(delta)) > 300 :
228
- print("The trim is larger than 5 minutes")
229
- is_wrong = True
230
- return is_wrong
231
-
232
- elif int(to_seconds(delta)) > video_length :
233
- print("The trim is larger than video lenght")
234
- is_wrong = True
235
- return is_wrong
236
- else:
237
- return is_wrong
238
-
239
- else:
240
- print("Your time format is invalid")
241
- is_wrong = True
242
- return is_wrong