-
Notifications
You must be signed in to change notification settings - Fork 5
/
YoutubeVideoToAIPoweredShorts.py
124 lines (75 loc) · 3.78 KB
/
YoutubeVideoToAIPoweredShorts.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
import pytube
from moviepy.editor import *
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
import pytube
import speech_recognition as sr
import youtube_transcript_api
import time
# Get the video URL from the user.
link = input("Enter the YouTube video link: ")
# Create a YouTube object using the provided link.
youtube = pytube.YouTube(link)
# Select the highest resolution video stream available.
video_stream = youtube.streams.get_highest_resolution()
# Set the output directory path.
output_dir_path = "Insert folder one path here"
# Download the video to the output directory path.
if video_stream:
video_stream.download(output_dir_path)
# Get the transcript of the YouTube video with the provided video ID.
def get_transcript(video_id):
transcript = youtube_transcript_api.YouTubeTranscriptApi.get_transcript(video_id)
text = ""
for line in transcript:
text += line['text'] + " "
return text
# Get the video ID from the YouTube link.
def get_video_id(link):
video_id = link.split("v=")[1]
return video_id
# Get the YouTube link from the user.
# Get the transcript of the YouTube video.
video_id = get_video_id(link)
transcript = get_transcript(video_id)
print("GET READY FOR PROMPT")
# Add "hello world" to the beginning of the transcript.
transcript = "Hello AI I would like you to help me find out which 59 second clip from this youtube transcript would work best, you must judge out of this entire transcript where 59 seoncds starts and where it ends, this marks one clip. so every time this happens you know this is part one, then part tow then part three etc. You must look at these clips and tell the user with quoted examples which one is the best and which one is best for youtube. you must also answer the number of the chronilogical clip Ex: (script) answer yes, clip 4 is the best (quote) also list the part number. " + transcript
time.sleep(10)
# Print the transcript.
print(transcript)
time.sleep(10)
input_dir = r"insert folder one path here"
output_dir = r"insert folder two path here"
# Find the first .mp4 file in the input directory
input_file = next((os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith(".mp4")), None)
if input_file is None:
print("No .mp4 files found in input directory")
else:
# Use ffmpeg to crop the video to a 9:16 aspect ratio
os.system(f'ffmpeg -i "{input_file}" -filter:v "crop=in_h*9/16:in_h" -c:a copy "{os.path.join(output_dir, os.path.basename(input_file))}"')
print("Video cropped and saved to output directory")
input_dir = r"insert folder two path here"
output_dir = r"insert folder three path here"
# Find all .mp4 files in the input directory
input_files = [os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith(".mp4")]
if not input_files:
print("No .mp4 files found in input directory")
else:
for input_file in input_files:
# Load the video file using moviepy
video = VideoFileClip(input_file)
# Calculate the number of 59-second clips in the video
num_clips = int(video.duration // 59)
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Iterate over each clip and save it to the output directory
for i in range(num_clips):
start_time = i * 59
end_time = min((i + 1) * 59, video.duration)
clip = video.subclip(start_time, end_time)
output_file_path = os.path.join(output_dir, f"{i+1}.mp4")
clip.write_videofile(output_file_path)
# Close the video file
video.close()
print("Videos cropped and saved to output directory")