import shutil import re import time from pathlib import Path from openaihelper import…
- User
import shutil import re import time from pathlib import Path from openai_helper import openai, generate_description from video_creation import create_final_clip from moviepy.editor import VideoFileClip
Set the directory For Ready To Edit Videos
folder_path = Path('E:/SMART AI BOTS/Selected')
Initialize counter and start time
num_videos_created = 0 start_time = time.time()
Loop through all video files in the folder
for video_path in folder_path.glob('*.mp4'): # Check the duration of the video with VideoFileClip(str(video_path)) as video: duration = video.duration if duration > 60: print(f"Skipping video {video_path} because it's longer than 60 seconds") #TODO: speed up video continue
# Extract variables from the filename name = video_path.stem channel, desc_hashtags = name.split('-', 1) desc_end_index = desc_hashtags.find('#') if desc_end_index != -1: desc = desc_hashtags[:desc_end_index].strip() hashtags = desc_hashtags[desc_end_index:].strip() else: desc = desc_hashtags.strip() hashtags = '' # Generate a description using ChatGPT prompt = f"Write a short, attention-grabbing, clickbaity description for a video as a statement. Use maximum 8 words and provide only one final result, all capital letters. This is the title: {desc_hashtags}" description = generate_description(prompt) print(f"Description: {description}") # Remove the first word if it's not capitalized to avoid promt bugs description= re.sub(r'^[^A-Z]+', '', description) print(f"Description after removal: {description}") # Remove double quotes from the description description= description.replace('"', '') # Clean up the description for use as a file name clean_description = re.sub(r'[^\w\s#-&:]', '', f'{description} {hashtags}') clean_description = re.sub(r'\s{2,}', '', clean_description.strip()) clean_description = re.sub(r'#+', '#', clean_description.strip()) clean_description = re.sub(r':', '-', clean_description.strip()) trimmed_file_name = f'{clean_description}.mp4' #Move video to Used folder print(f"Moving video {video_path} to Used folder") used_path = folder_path / 'Used' / video_path.name shutil.move(video_path, used_path) print(f"Video moved to Used folder: {used_path}") # Edit the video final_clip = create_final_clip(description, used_path) # # Trim the final clip to 2 seconds # final_clip = final_clip.subclip(0, 2) # Save final video to final folder final_path = folder_path / 'Final' / f'{desc_hashtags}.mp4'#trimmed_file_name final_clip.write_videofile(str(final_path), preset="ultrafast",codec="libx264", threads=4) # Increment counter num_videos_created += 1 # Close the clip final_clip.close()Calculate elapsed time
elapsed_time = time.time() - start_time
Print summary
print(f"Created {num_videos_created} videos in {elapsed_time/60:.2f} minutes")
I need to create function, that takes video_path as imput to refer to video. Then function chosese audio file of the video and turn audio into text and saves as variable video_text
- Assistant
You can use the
speech_recognitionlibrary to extract audio from the video file and convert it to text.First, you need to install the required libraries:
speech_recognitionandpydub. Run the following commands in your terminal or command prompt:pip install SpeechRecognition pip install pydubAfter installing the dependencies, use the following function:
import speech_recognition as sr from pydub import AudioSegment def video_to_text(video_path): # Extract audio from video video = VideoFileClip(str(video_path)) audio = video.audio audio_path = video_path.stem + ".wav" audio.write_audiofile(audio_path) # Convert audio to text using SpeechRecognition recognizer = sr.Recognizer() audio_segment = AudioSegment.from_wav(audio_path) # Check if audio file duration is less than the limit of the library (2 min) if audio_segment.duration_seconds > 120: raise Exception("Audio duration is longer than 120 seconds") with sr.AudioFile(audio_path) as src_file: audio_data = recognizer.record(src_file) video_text = recognizer.recognize_google(audio_data) return video_text video_path = "path/to/your/video.mp4" video_text = video_to_text(video_path) print("Video text:", video_text)