amit0987 commited on
Commit
1a33f31
Β·
1 Parent(s): 4feb422

Create app.py

Browse files
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ /content
2
+ /youtube_videos
3
+ /.idea
4
+ /Unique_code
5
+ /output_videos
ImportantVariables.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import json
4
+
5
+ def read_json_file(input_file_path):
6
+ with open(input_file_path, 'r') as file:
7
+ data = json.load(file) # Load the JSON data from the file
8
+ return data
9
+
10
+ # root folder path
11
+ file_path = os.path.abspath(__file__)
12
+ current_Folder_Path = os.path.dirname(file_path)
13
+ root_folder = os.path.dirname(current_Folder_Path)
14
+
15
+ # imp folder path
16
+ content_folder = current_Folder_Path + "/content/"
17
+ unique_code_file_Path=current_Folder_Path +"/Unique_code/unique_code.json"
18
+
19
+ # drive imp folders id
20
+ youtube_videos_for_upload_folder_id = "150E6lc1owcHdPnz5DKOFXgmSxjFm0i3f"
21
+ content_folder_id = "1tcNOWyaTxPrnEfDkTlejSySNQE3Ib9BT"
22
+ unicode_folder_id="1iOt0tn1xcbfOZGvUwkRsLSFQFusbJZL8"
23
+
24
+ download_drive_folder_list = [youtube_videos_for_upload_folder_id, content_folder_id]
25
+
26
+ # vedio editing paths
27
+ PATHS = {
28
+ # files path
29
+ "logo": content_folder + "logo.png",
30
+ "background": content_folder + "background_image.jpg",
31
+ "banner": content_folder + "yt_banner.png",
32
+ "subscribe": content_folder + "subscribe.png",
33
+ "animation": content_folder + "like_share_subscribe_animation.mp4",
34
+
35
+ # imp folder path
36
+ "input_folder_path": current_Folder_Path + "/youtube_videos/",
37
+ "output_folder_path": current_Folder_Path + "/output_videos/"
38
+
39
+ # "input_videos": current_Folder_Path + "input_vedio/",
40
+ # "audio": content_folder + "audio.mp3",
41
+ # "output_video": current_Folder_Path + "/output_videos/output_video_with_audio.mp4",
42
+ # "input_video_path": current_Folder_Path + "/downloaded_videos/vedio_0.webm",
43
+
44
+ }
45
+
46
+
47
+ # imp functions
48
+ def create_output_videos_folder():
49
+ output_videos_folder = PATHS["output_folder_path"]
50
+ if not os.path.exists(output_videos_folder):
51
+ os.makedirs(output_videos_folder)
52
+ return output_videos_folder
53
+
54
+
55
+
56
+
57
+
58
+ def check_paths_exist(paths):
59
+ ignore_keys = {"input_videos", "audio", "output_video", "input_video_path"} # Keys to ignore
60
+ missing_paths = {}
61
+ for key, path in paths.items():
62
+ if key not in ignore_keys and not os.path.exists(path):
63
+ missing_paths[key] = path
64
+
65
+ if missing_paths:
66
+ print("\033[91mThe following paths do not exist:\033[0m") # Red color
67
+ for key, path in missing_paths.items():
68
+ print(f"\033[91m{key}: {path}\033[0m")
69
+ raise Exception("Some required paths are missing.")
70
+ else:
71
+ print("All paths exist.")
72
+
73
+ return missing_paths
74
+
75
+ def validate_name(name):
76
+ json_data = read_json_file(unique_code_file_Path)
77
+ unique_code = json_data["unique_code"]
78
+ print(name)
79
+ if name == unique_code:
80
+ return True
81
+ else:
82
+ return False
83
+
84
+
app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import temp_app
4
+
5
+
6
+ def greet(name):
7
+ is_valid_name, validated_name = temp_app.validate_name(name)
8
+ if is_valid_name:
9
+ return validated_name
10
+ else:
11
+ return f"Hello, {name}!"
12
+
13
+
14
+ app = gr.Interface(fn=greet, inputs="text", outputs="text")
15
+
16
+ if __name__ == "__main__":
17
+ app.launch()
download_drive_folder.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Replace with your Google Drive folder ID
2
+ import gdown
3
+
4
+ import ImportantVariables
5
+
6
+ def download_folder_from_list(folder_list):
7
+
8
+ for folder_id in folder_list:
9
+ print(f"Downloading folder {folder_id}")
10
+ # Use gdown to download the folder
11
+
12
+ # Construct the full URL for the Google Drive folder
13
+ folder_url = f'https://drive.google.com/drive/folders/{folder_id}'
14
+
15
+ gdown.download_folder(folder_url)
16
+
17
+
18
+
19
+ def main():
20
+
21
+ download_drive_folder_list = ImportantVariables.download_drive_folder_list
22
+ download_folder_from_list(download_drive_folder_list)
23
+
24
+
25
+
26
+
27
+ if __name__ == "__main__":
28
+ main()
requirements.txt ADDED
Binary file (126 Bytes). View file
 
temp_app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ImportantVariables
2
+ import download_drive_folder
3
+ import vedio_upload_to_gofile
4
+ import youtube_vedio_edit
5
+
6
+
7
+ def validate_name(name):
8
+ download_drive_folder.download_folder_from_list([ImportantVariables.unicode_folder_id])
9
+ result = ImportantVariables.validate_name(name)
10
+ file_id= ""
11
+ if result:
12
+ file_id =main()
13
+ return result,file_id
14
+
15
+ def main():
16
+ download_drive_folder.main()
17
+ youtube_vedio_edit.main_for_me()
18
+ public_url=vedio_upload_to_gofile.main()
19
+ return public_url
20
+
21
+
22
+ if __name__ == "__main__":
23
+ print(validate_name("abcdef"))
24
+
25
+
vedio_upload_to_gofile.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import shutil
3
+ import os
4
+
5
+ import ImportantVariables
6
+
7
+
8
+ def upload_folder_to_gofile(folder_path, zip_name="folder.zip"):
9
+ # Compress the folder into a ZIP file
10
+ print("Compress the folder into a ZIP file")
11
+ zip_path = f"{zip_name}"
12
+ shutil.make_archive(zip_path.replace(".zip", ""), 'zip', folder_path)
13
+
14
+ # Upload the ZIP file to Gofile
15
+ print("Upload the ZIP file to Gofile")
16
+ url = "https://store1.gofile.io/uploadFile"
17
+ with open(zip_path, "rb") as f:
18
+ response = requests.post(url, files={"file": f})
19
+
20
+ # Handle response
21
+ if response.status_code == 200:
22
+ result = response.json()
23
+ if result["status"] == "ok":
24
+ public_url = result["data"]["downloadPage"]
25
+ print(f"βœ… Folder uploaded: {public_url}")
26
+ # Cleanup: Remove the ZIP file after upload
27
+ os.remove(zip_path)
28
+ return public_url
29
+ else:
30
+ print("❌ Upload failed:", result)
31
+ else:
32
+ print(f"❌ Error: HTTP {response.status_code}")
33
+
34
+
35
+
36
+
37
+
38
+ def main():
39
+ # Example usage
40
+ folder_path = ImportantVariables.PATHS["output_folder_path"]
41
+ public_url=upload_folder_to_gofile(folder_path)
42
+ return public_url
43
+
44
+
45
+ if __name__ == "__main__":
46
+ main()
47
+
48
+
youtube_vedio_edit.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from moviepy.editor import (VideoFileClip, ImageClip, CompositeVideoClip, AudioFileClip, concatenate_videoclips)
4
+
5
+ import ImportantVariables
6
+ from ImportantVariables import PATHS, check_paths_exist
7
+ from moviepy.editor import ColorClip
8
+
9
+ # Add background image behind the video
10
+ def add_bg_image_on_video(video_clip, image_clip, target_width=1920, target_height=1080):
11
+ print("add_bg_image_on_video")
12
+ final_clip = CompositeVideoClip([image_clip, video_clip.set_position(("center", "center"))])
13
+ return final_clip
14
+
15
+ def find_width_of_x(width,height):
16
+ x=int((width-height*(9/16))/2)
17
+ return x
18
+
19
+ # Add an image (logo/banner/subscribe button) on top of a video
20
+ def add_image_to_video(shorts_video_clip, image_clip, width_of_image, height_of_image=0, position=(0, 0)):
21
+ print("add_image_to_video")
22
+ if height_of_image == 0:
23
+ image_clip = image_clip.resize(width=width_of_image)
24
+ else:
25
+ image_clip = image_clip.resize(width=width_of_image, height=height_of_image)
26
+
27
+ final_clip = CompositeVideoClip([shorts_video_clip, image_clip.set_position(position)])
28
+ return final_clip
29
+
30
+
31
+ # Add logo to the video
32
+ def add_logo_to_video(final_shorts_video_clip, logo_image_clip, bg_image_clip_width, shorts_video_clip_width):
33
+ print("add_logo_to_video")
34
+ width_of_image = (bg_image_clip_width - shorts_video_clip_width) / 2
35
+ height_of_image = 0
36
+ x = 0
37
+ y = "center"
38
+ position = (x, y)
39
+
40
+ if width_of_image >= (final_shorts_video_clip.h // 4):
41
+ final_clip = add_image_to_video(final_shorts_video_clip, logo_image_clip, width_of_image, height_of_image,
42
+ position)
43
+ return final_clip
44
+ else:
45
+ return final_shorts_video_clip
46
+
47
+
48
+ # Add banner to the video
49
+ def add_banner_to_video(final_shorts_video_clip, banner_image_clip):
50
+ print("add_banner_to_video")
51
+
52
+ w,h =final_shorts_video_clip.size
53
+ print(w,h)
54
+ width_of_image=find_width_of_x(w,h)
55
+ print("width_of_image",width_of_image)
56
+ height_of_image = 0
57
+ x = "right"
58
+ y = "top"
59
+ position = (x, y)
60
+
61
+ final_clip = add_image_to_video(final_shorts_video_clip, banner_image_clip, width_of_image, height_of_image,
62
+ position)
63
+ return final_clip
64
+
65
+
66
+ # Add subscribe button to the video
67
+ def add_subscribe_png(final_shorts_video_clip, subscribe_image_clip, bg_image_clip_width, shorts_video_clip_width):
68
+ print("add_subscribe_png")
69
+ width_of_image = (bg_image_clip_width - shorts_video_clip_width) / 2
70
+ height_of_image = 0
71
+ x = "right"
72
+ y = "center"
73
+ position = (x, y)
74
+
75
+ if width_of_image >= (final_shorts_video_clip.h // 4):
76
+ final_clip = add_image_to_video(final_shorts_video_clip, subscribe_image_clip, width_of_image, height_of_image,
77
+ position)
78
+ return final_clip
79
+ else:
80
+ return final_shorts_video_clip
81
+
82
+
83
+ def add_like_share_subscribe_animation(final_shorts_video_clip, animation_clip_path):
84
+ try:
85
+ print("add_like_share_subscribe_animation")
86
+
87
+ # Load the animation clip
88
+
89
+ animation_clip = VideoFileClip(animation_clip_path)
90
+
91
+ # Repeat the animation clip every 10 seconds
92
+ main_video_duration = final_shorts_video_clip.duration
93
+ repeated_clips = []
94
+ start_time = 0
95
+
96
+ while start_time < main_video_duration:
97
+ clip = animation_clip.set_start(start_time).set_duration(
98
+ min(animation_clip.duration, main_video_duration - start_time))
99
+ repeated_clips.append(clip)
100
+ start_time += 10 # Add animation every 10 seconds
101
+
102
+ # Combine all repeated animations
103
+ animation_sequence = concatenate_videoclips(repeated_clips)
104
+ w,h=final_shorts_video_clip.size
105
+
106
+ # Resize the animation clip if necessary
107
+ animation_sequence = animation_sequence.resize(height=h/5.1) # Example: Resize to a height of 100px
108
+ position = ("center", "bottom") # Position: Centered horizontally, bottom vertically
109
+
110
+ # Overlay the animation onto the main video
111
+ final_clip = CompositeVideoClip([final_shorts_video_clip, animation_sequence.set_position(position)])
112
+
113
+ return final_clip
114
+ except Exception as e:
115
+ print(f"Error adding animation: {e}")
116
+ return final_shorts_video_clip
117
+
118
+
119
+ # Main function to process a single video
120
+ def add_main_features(shorts_video_path):
121
+ global shorts_video_clip
122
+ dimensions = get_video_dimensions(shorts_video_path)
123
+
124
+ if dimensions:
125
+ width, height = dimensions
126
+ if is_16_9(width, height):
127
+ print(f"βœ… Video is 16:9. Cropping to 9:16...")
128
+ shorts_video_clip = crop_to_9_16(shorts_video_path)
129
+ else:
130
+ print(f"❌ Video is not 16:9, no cropping needed.")
131
+ shorts_video_clip = VideoFileClip(shorts_video_path)
132
+ print(f"βœ… Video Clip Loaded: {shorts_video_clip}")
133
+
134
+
135
+ max_duration = 0.5
136
+ # clip_duration = min(max_duration, shorts_video_clip.duration)
137
+ # shorts_video_clip = shorts_video_clip.subclip(0, clip_duration)
138
+
139
+ bg_image_clip = ImageClip(PATHS["background"]).set_duration(shorts_video_clip.duration)
140
+ banner_image_clip = ImageClip(PATHS["banner"]).set_duration(shorts_video_clip.duration)
141
+ logo_image_clip = ImageClip(PATHS["logo"]).set_duration(shorts_video_clip.duration)
142
+ subscribe_png_clip = ImageClip(PATHS["subscribe"]).set_duration(shorts_video_clip.duration)
143
+ print("subscribe_png_clip",subscribe_png_clip)
144
+ print("πŸ” bg_image_clip before resize:", bg_image_clip)
145
+ print(f"πŸ” Background image size: {bg_image_clip.size}")
146
+
147
+
148
+ bg_image_clip = ColorClip(size=(1280, 720), color=(255, 0, 0)) # Red background
149
+ bg_image_clip = bg_image_clip.set_duration(shorts_video_clip.duration)
150
+ print("βœ… Test background created")
151
+ print("βœ… bg_image_clip resized successfully!") # If this doesn't print, script is freezing here
152
+ print("πŸ” bg_image_clip after resize:", bg_image_clip)
153
+
154
+ bg_image_clip_width = bg_image_clip.w
155
+ shorts_video_clip_width = shorts_video_clip.w
156
+ print("shorts_video_clip_width",shorts_video_clip_width)
157
+
158
+
159
+ # Add background image
160
+ final_shorts_video_clip = add_bg_image_on_video(shorts_video_clip, bg_image_clip)
161
+ # Add logo
162
+ final_shorts_video_clip = add_logo_to_video(final_shorts_video_clip, logo_image_clip, bg_image_clip_width,shorts_video_clip_width)
163
+ # Add banner
164
+ final_shorts_video_clip = add_banner_to_video(final_shorts_video_clip, banner_image_clip)
165
+ # Add subscribe png
166
+ final_shorts_video_clip = add_subscribe_png(final_shorts_video_clip, subscribe_png_clip, bg_image_clip_width,shorts_video_clip_width)
167
+
168
+ return final_shorts_video_clip
169
+
170
+
171
+ # Concatenate multiple videos
172
+ def concatenate_videos(video_directory):
173
+ video_clips = []
174
+ target_width = 1920
175
+ target_height = 1080
176
+
177
+ for filename in os.listdir(video_directory):
178
+ if filename.endswith(('.mp4', '.avi', '.mov')):
179
+ video_path = os.path.join(video_directory, filename)
180
+ print(f"Processing: {video_path}")
181
+ try:
182
+ video_clip = add_main_features(video_path)
183
+ if video_clip and video_clip.duration > 0:
184
+ video_clip = video_clip.without_audio()
185
+ video_clip = video_clip.resize(width=target_width, height=target_height)
186
+ video_clip = video_clip.set_fps(30)
187
+ video_clips.append(video_clip)
188
+ else:
189
+ print(f"Skipping invalid clip: {video_path}")
190
+ except Exception as e:
191
+ print(f"Error processing {video_path}: {e}")
192
+
193
+ if video_clips:
194
+ print(f"Concatenating {len(video_clips)} video clips...")
195
+ final_clip = concatenate_videoclips(video_clips, method="compose")
196
+ return final_clip
197
+ else:
198
+ print("No valid video files found in the directory.")
199
+ return None
200
+
201
+
202
+ # Add audio to concatenated video
203
+ def concatenate_videos_with_audio(video_directory, audio_file):
204
+ audio_clip = AudioFileClip(audio_file)
205
+ video_clip = concatenate_videos(video_directory)
206
+
207
+ animation_clip_path = PATHS["animation"]
208
+ video_clip = add_like_share_subscribe_animation(video_clip, animation_clip_path)
209
+
210
+ if video_clip is None:
211
+ raise ValueError("No valid video clips to concatenate.")
212
+
213
+ if audio_clip.duration < video_clip.duration:
214
+ audio_clip = audio_clip.set_duration(video_clip.duration)
215
+ elif video_clip.duration < audio_clip.duration:
216
+ audio_clip = audio_clip.set_duration(video_clip.duration)
217
+
218
+ # video_clip = video_clip.set_audio(audio_clip)
219
+ return video_clip
220
+
221
+
222
+ # Main function
223
+ def main():
224
+ video_directory = PATHS["input_videos"]
225
+ audio_file = PATHS["audio"]
226
+
227
+ final_clip = concatenate_videos_with_audio(video_directory, audio_file)
228
+ final_clip.write_videofile(PATHS["output_video"], codec="libx264", audio_codec="aac")
229
+ print("Video created successfully!")
230
+
231
+
232
+ def main_for_one_vedio(input_video_path,output_vedio_path):
233
+ missing_paths = check_paths_exist(PATHS)
234
+ print(missing_paths)
235
+
236
+
237
+ animation_clip_path = PATHS["animation"]
238
+ print(animation_clip_path)
239
+ final_clip = add_main_features(input_video_path)
240
+ print("hello")
241
+ # final_clip = add_like_share_subscribe_animation(final_clip, animation_clip_path)
242
+ final_clip.write_videofile(output_vedio_path, codec="libx264", audio_codec="aac")
243
+ print("Video created successfully!")
244
+
245
+
246
+ def get_video_dimensions(video_path):
247
+ clip = VideoFileClip(video_path)
248
+ width, height = clip.size # Get (width, height)
249
+ clip.close()
250
+ print(width, height)
251
+ return width, height
252
+
253
+
254
+ def is_16_9(width, height):
255
+ aspect_ratio = width / height
256
+ print(aspect_ratio)
257
+ return abs(aspect_ratio - (16 / 9)) < 0.01
258
+
259
+
260
+ def crop_to_9_16(video_path):
261
+ clip = VideoFileClip(video_path)
262
+ width, height = clip.size
263
+
264
+ new_width = int(height * 9 / 16) # Calculate new width for 9:16
265
+ crop_x = (width - new_width) // 2 # Crop equally from left & right
266
+
267
+ cropped_clip = clip.crop(x1=crop_x, x2=crop_x + new_width)
268
+ return cropped_clip
269
+
270
+ def main_for_many_vedios(input_folder_path,output_folder_path):
271
+ print("hello")
272
+ for filename in os.listdir(input_folder_path):
273
+ if filename.endswith(".mp4"):
274
+ input_video_path = os.path.join(input_folder_path, filename)
275
+ name, _ = os.path.splitext(filename)
276
+ output_video_path = os.path.join(output_folder_path, f"{filename}_process.mp4")
277
+ main_for_one_vedio(input_video_path, output_video_path)
278
+ print(f"Processed {filename}")
279
+
280
+ def main_for_me():
281
+
282
+ input_video_folder_path = PATHS["input_folder_path"]
283
+ output_video_folder_path=ImportantVariables.create_output_videos_folder()
284
+ main_for_many_vedios(input_video_folder_path,output_video_folder_path)
285
+
286
+ if __name__ == "__main__":
287
+ main_for_me()