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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""ConfusedAutoShortVideoGen.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1qGRLgmJahs6-cNBhO_SIsz_yXKz2OqEW
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install gradio
|
| 11 |
+
!pip install gradio_client
|
| 12 |
+
!pip install whisperx
|
| 13 |
+
!pip install pydub
|
| 14 |
+
|
| 15 |
+
## Menu
|
| 16 |
+
##script_writing.py
|
| 17 |
+
|
| 18 |
+
mscript_input = "what is depression"
|
| 19 |
+
mscript_music_input = "What is depression"
|
| 20 |
+
final_video_output = "final_video_output.mp4"
|
| 21 |
+
musicownpath = '/content/tmp1mbn3d3s.mp4'
|
| 22 |
+
|
| 23 |
+
"""Here’s a consistent and well-structured format where each script name aligns with the key variables:
|
| 24 |
+
|
| 25 |
+
**script_writing.py**
|
| 26 |
+
|
| 27 |
+
input_text (user_input)
|
| 28 |
+
script_output (scripttxt_output)
|
| 29 |
+
script_text (text)
|
| 30 |
+
audio_gen.py
|
| 31 |
+
|
| 32 |
+
script_audio_input (scripttxt_input)
|
| 33 |
+
audio_file (audio_output)
|
| 34 |
+
audio_format (mp3)
|
| 35 |
+
music_gen.py
|
| 36 |
+
|
| 37 |
+
script_music_input (scripttxt_input)
|
| 38 |
+
music_file (music_output)
|
| 39 |
+
music_format (mp3)
|
| 40 |
+
time_stamp_code.py
|
| 41 |
+
|
| 42 |
+
input_audio (audio_input)
|
| 43 |
+
timestamp_csv (tscsv_output)
|
| 44 |
+
csv_format (csv)
|
| 45 |
+
common_words_remover.py
|
| 46 |
+
|
| 47 |
+
raw_csv (csv_input)
|
| 48 |
+
filtered_csv (commoncsv_output)
|
| 49 |
+
csv_data (csv)
|
| 50 |
+
giphy_download.py
|
| 51 |
+
|
| 52 |
+
input_keywords (commoncsv_input)
|
| 53 |
+
downloaded_gif (gif_output)
|
| 54 |
+
gif_format (gif)
|
| 55 |
+
gif_crop_concat.py
|
| 56 |
+
|
| 57 |
+
raw_gif (gif_input)
|
| 58 |
+
gif_keywords (commoncsv_input)
|
| 59 |
+
final_video (concatenate_cropped_output)
|
| 60 |
+
video_format (mp4)
|
| 61 |
+
video_finalizer.py
|
| 62 |
+
|
| 63 |
+
video_input (concatenate_cropped_input)
|
| 64 |
+
audio_track (audio_input)
|
| 65 |
+
timestamps (tscsv_input)
|
| 66 |
+
background_music (music_input)
|
| 67 |
+
output_video (final_video_output)
|
| 68 |
+
output_format (mp4)
|
| 69 |
+
|
| 70 |
+
### script_writing.py
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
import csv
|
| 74 |
+
import re
|
| 75 |
+
from datetime import datetime
|
| 76 |
+
from gradio_client import Client
|
| 77 |
+
|
| 78 |
+
# Initialize the client with the correct Hugging Face Space
|
| 79 |
+
client = Client("Abu1998/Meme_finder")
|
| 80 |
+
|
| 81 |
+
# Define the system message and input sentence
|
| 82 |
+
system_message = """Task: Act as a YouTube Shorts content writer.
|
| 83 |
+
|
| 84 |
+
Objective: Create engaging, catchy, and trendy scripts for YouTube Shorts videos that are brief, attention-grabbing, and optimized for viral potential.
|
| 85 |
+
|
| 86 |
+
Guidelines:
|
| 87 |
+
|
| 88 |
+
Each script should be 15-30 seconds long.
|
| 89 |
+
Use a hook in the first few seconds to capture viewers' attention.
|
| 90 |
+
Ensure the content is aligned with trending topics, challenges, or popular culture.
|
| 91 |
+
Incorporate humor, relatable scenarios, or strong emotions to resonate with the audience.
|
| 92 |
+
End with a clear call-to-action (CTA) like “Follow for more!” or a cliffhanger.
|
| 93 |
+
Example Flow:
|
| 94 |
+
|
| 95 |
+
User Input: “Write a script about the Monday blues.”
|
| 96 |
+
AI Output:
|
| 97 |
+
Script: "POV: It’s Monday morning, and you’re already done with the week. [Clip shows someone groggily hitting the snooze button, dragging themselves out of bed]. But wait… there’s coffee. And suddenly, everything’s okay! ☕✨ [Cut to a quick burst of energy with upbeat music]. If you’re just surviving till the weekend, hit that follow button for more relatable vibes!"
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
# Define the user input (the sentence for which you want to find the main keyword)
|
| 101 |
+
user_input = mscript_input
|
| 102 |
+
|
| 103 |
+
# Make the API call with the specified parameters
|
| 104 |
+
result = client.predict(
|
| 105 |
+
message=user_input,
|
| 106 |
+
system_message=system_message,
|
| 107 |
+
max_tokens=512,
|
| 108 |
+
temperature=0.7,
|
| 109 |
+
top_p=0.95,
|
| 110 |
+
api_name="/chat"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Extract the script from the result
|
| 114 |
+
script = result.strip()
|
| 115 |
+
|
| 116 |
+
# Function to split script into words
|
| 117 |
+
def split_into_words(script_text):
|
| 118 |
+
words = re.findall(r'\w+', script_text) # Find all words
|
| 119 |
+
return words
|
| 120 |
+
|
| 121 |
+
# Convert the script to a list of words
|
| 122 |
+
words = split_into_words(script)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# Define the file names with timestamp
|
| 126 |
+
csv_file = f'updates.csv'
|
| 127 |
+
txt_file = f'script_output'
|
| 128 |
+
|
| 129 |
+
# Save to CSV
|
| 130 |
+
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
|
| 131 |
+
writer = csv.writer(file)
|
| 132 |
+
writer.writerow(['Content', 'Word']) # Headers
|
| 133 |
+
for word in words:
|
| 134 |
+
writer.writerow([user_input, word]) # Write each word as a separate row
|
| 135 |
+
|
| 136 |
+
print(f"Script generated, split into words, and saved to {csv_file}.")
|
| 137 |
+
|
| 138 |
+
# Save to TXT
|
| 139 |
+
with open(txt_file, mode='w', encoding='utf-8') as file:
|
| 140 |
+
file.write(script)
|
| 141 |
+
|
| 142 |
+
print(f"Script saved to {txt_file}.")
|
| 143 |
+
|
| 144 |
+
"""### audio_gen.py"""
|
| 145 |
+
|
| 146 |
+
# Install the gradio_client library
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
from gradio_client import Client
|
| 150 |
+
from google.colab import files
|
| 151 |
+
import shutil
|
| 152 |
+
|
| 153 |
+
# Initialize the client with the correct Hugging Face Space
|
| 154 |
+
client = Client("innoai/Edge-TTS-Text-to-Speech")
|
| 155 |
+
|
| 156 |
+
# Upload the script file
|
| 157 |
+
file_path = "/content/script_output"
|
| 158 |
+
|
| 159 |
+
# Read the content from the uploaded script file
|
| 160 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 161 |
+
text_input = file.read().strip() # Read and strip any extra whitespace
|
| 162 |
+
|
| 163 |
+
# Make the API call with the file content as input
|
| 164 |
+
result = client.predict(
|
| 165 |
+
text=text_input,
|
| 166 |
+
voice="en-US-AvaMultilingualNeural - en-US (Female)", # You can change the voice as needed
|
| 167 |
+
rate=0, # You can adjust the speech rate if needed
|
| 168 |
+
pitch=0, # You can adjust the pitch if needed
|
| 169 |
+
api_name="/predict"
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Check the result type and content
|
| 173 |
+
print(result)
|
| 174 |
+
|
| 175 |
+
# Extract the local file path from the result
|
| 176 |
+
audio_file_path = result[0] # Assuming the audio file path is the first element
|
| 177 |
+
|
| 178 |
+
# Define the output file name and path
|
| 179 |
+
output_file_path = "/content/audio_output.mp3"
|
| 180 |
+
|
| 181 |
+
# Copy the file to the desired location
|
| 182 |
+
shutil.copy(audio_file_path, output_file_path)
|
| 183 |
+
|
| 184 |
+
# Provide download link for the generated audio file
|
| 185 |
+
#files.download(output_file_path)
|
| 186 |
+
|
| 187 |
+
"""###Music Gen"""
|
| 188 |
+
|
| 189 |
+
#from gradio_client import Client
|
| 190 |
+
#import requests
|
| 191 |
+
|
| 192 |
+
# Initialize the client with the correct Hugging Face Space URL
|
| 193 |
+
# Make sure the URL is correct and points to a valid Gradio app.
|
| 194 |
+
#client = Client("https://huggingface.co/spaces/facebook/MusicGen") # Updated URL
|
| 195 |
+
|
| 196 |
+
# Define the input for the API
|
| 197 |
+
#input_text = "youtube shorts background music, beatbox, loop"
|
| 198 |
+
|
| 199 |
+
# Query the API (fn_index=2 is used based on the provided information)
|
| 200 |
+
#result = client.predict(
|
| 201 |
+
# input_text, # str input in 'parameter_16' Dataset component
|
| 202 |
+
# fn_index=2
|
| 203 |
+
#)
|
| 204 |
+
|
| 205 |
+
# Check the result type
|
| 206 |
+
#print(result)
|
| 207 |
+
|
| 208 |
+
# Extract the URL from the result
|
| 209 |
+
# Assuming result is a tuple where the audio URL is the second element
|
| 210 |
+
#audio_url = result[1]
|
| 211 |
+
|
| 212 |
+
# Define the output file path
|
| 213 |
+
#output_file_path = "/content/tmp1mbn3d3s.mp4"
|
| 214 |
+
|
| 215 |
+
# Download the audio content from the URL
|
| 216 |
+
#response = requests.get(audio_url)
|
| 217 |
+
#audio_content = response.content
|
| 218 |
+
|
| 219 |
+
# Save the audio output to the specified path
|
| 220 |
+
#with open(output_file_path, 'wb') as f:
|
| 221 |
+
# f.write(audio_content)
|
| 222 |
+
|
| 223 |
+
# Provide download link for the generated audio file
|
| 224 |
+
#from google.colab import files
|
| 225 |
+
#files.download(output_file_path)
|
| 226 |
+
|
| 227 |
+
"""### Time Stamp"""
|
| 228 |
+
|
| 229 |
+
!pip install whisperx
|
| 230 |
+
|
| 231 |
+
import whisperx
|
| 232 |
+
import torch
|
| 233 |
+
import pandas as pd
|
| 234 |
+
|
| 235 |
+
# Initialize the WhisperX model
|
| 236 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 237 |
+
compute_type = "float32" if device == "cpu" else "float16"
|
| 238 |
+
model = whisperx.load_model("large-v2", device, compute_type=compute_type)
|
| 239 |
+
|
| 240 |
+
def transcribe_and_align(audio_file):
|
| 241 |
+
# Load audio
|
| 242 |
+
audio = whisperx.load_audio(audio_file)
|
| 243 |
+
print("Audio loaded successfully.")
|
| 244 |
+
|
| 245 |
+
# Transcribe
|
| 246 |
+
result = model.transcribe(audio, batch_size=16)
|
| 247 |
+
print("Transcription result:", result)
|
| 248 |
+
|
| 249 |
+
# Align transcription
|
| 250 |
+
model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
|
| 251 |
+
result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=True)
|
| 252 |
+
print("Alignment result:", result)
|
| 253 |
+
|
| 254 |
+
# Process segments to get word-level timestamps
|
| 255 |
+
word_segments = []
|
| 256 |
+
for segment in result["segments"]:
|
| 257 |
+
for word_info in segment.get("words", []): # Ensure 'words' is used
|
| 258 |
+
if "word" in word_info and "start" in word_info and "end" in word_info:
|
| 259 |
+
word_segments.append({
|
| 260 |
+
"word": word_info["word"],
|
| 261 |
+
"start": word_info["start"],
|
| 262 |
+
"end": word_info["end"],
|
| 263 |
+
"duration": word_info["end"] - word_info["start"]
|
| 264 |
+
})
|
| 265 |
+
|
| 266 |
+
# Debug: Print word segments to check if they are being populated
|
| 267 |
+
print("Word segments:", word_segments)
|
| 268 |
+
|
| 269 |
+
# Convert the word segments to a DataFrame
|
| 270 |
+
df = pd.DataFrame(word_segments)
|
| 271 |
+
|
| 272 |
+
# Save the result to a CSV file
|
| 273 |
+
output_file = "/content/transcription_with_word_timestamps.csv" # Ensure correct file path
|
| 274 |
+
df.to_csv(output_file, index=False)
|
| 275 |
+
|
| 276 |
+
return output_file
|
| 277 |
+
|
| 278 |
+
# Provide the path to your audio file
|
| 279 |
+
audio_file_path = "/content/audio_output.mp3"
|
| 280 |
+
# Transcribe and align the audio file
|
| 281 |
+
output_file = transcribe_and_align(audio_file_path)
|
| 282 |
+
|
| 283 |
+
# Print the path to the output file
|
| 284 |
+
print(f"Word-level transcription with timestamps saved to: {output_file}")
|
| 285 |
+
|
| 286 |
+
"""### common_words_remover"""
|
| 287 |
+
|
| 288 |
+
# prompt: write a code to drop these common words from output_file word column , COMMON_WORDS = {"the", "and", "is", "in", "to", "of", "a", "with", "for", "on", "it", "as", "at", "by", "an","this", "that", "which", "or", "be", "are", "was", "were", "has", "have", "had", "why", "such","here", "some", "so", "easy"}
|
| 289 |
+
|
| 290 |
+
import pandas as pd
|
| 291 |
+
def drop_common_words(input_file, output_file, common_words):
|
| 292 |
+
"""
|
| 293 |
+
Drops rows containing common words in the 'word' column and saves the result to a new CSV file.
|
| 294 |
+
|
| 295 |
+
Args:
|
| 296 |
+
input_file (str): The path to the input CSV file.
|
| 297 |
+
output_file (str): The path to the output CSV file.
|
| 298 |
+
common_words (set): A set of common words to be removed.
|
| 299 |
+
"""
|
| 300 |
+
df = pd.read_csv(input_file)
|
| 301 |
+
df['word'] = df['word'].str.lower() # Convert words to lowercase for comparison
|
| 302 |
+
df = df[~df['word'].isin(common_words)] # Filter out rows with common words
|
| 303 |
+
df.to_csv(output_file, index=False)
|
| 304 |
+
|
| 305 |
+
# Set of common words to drop
|
| 306 |
+
COMMON_WORDS = {"the", "and", "is", "in", "to", "of", "a", "with", "for", "on", "it", "as", "at", "by", "an","this", "that", "which", "or", "be", "are", "was", "were", "has", "have", "had", "why", "such","here", "some", "so", "easy"}
|
| 307 |
+
|
| 308 |
+
# Input and output file paths
|
| 309 |
+
input_file = "/content/transcription_with_word_timestamps.csv"
|
| 310 |
+
output_file = "/content/filtered_transcription.csv"
|
| 311 |
+
|
| 312 |
+
# Call the function to drop common words
|
| 313 |
+
drop_common_words(input_file, output_file, COMMON_WORDS)
|
| 314 |
+
|
| 315 |
+
print(f"Rows with common words dropped and saved to {output_file}")
|
| 316 |
+
|
| 317 |
+
"""### common_words_remover 2nd step"""
|
| 318 |
+
|
| 319 |
+
import pandas as pd
|
| 320 |
+
from pydub import AudioSegment
|
| 321 |
+
|
| 322 |
+
def update_dataframe_with_audio_duration(csv_file, audio_file):
|
| 323 |
+
# Load the CSV file into a DataFrame
|
| 324 |
+
df = pd.read_csv(csv_file)
|
| 325 |
+
|
| 326 |
+
# Calculate the total duration of the audio
|
| 327 |
+
audio = AudioSegment.from_file(audio_file)
|
| 328 |
+
total_duration = audio.duration_seconds
|
| 329 |
+
|
| 330 |
+
# Drop existing 'end' and 'duration' columns
|
| 331 |
+
df = df.drop(columns=['end', 'duration'], errors='ignore')
|
| 332 |
+
|
| 333 |
+
# Create a new 'end' column with the next 'start' value
|
| 334 |
+
df['end'] = df['start'].shift(-1)
|
| 335 |
+
|
| 336 |
+
# The first row should start with 0.01
|
| 337 |
+
df.loc[0, 'start'] = 0.01
|
| 338 |
+
|
| 339 |
+
# The last row's 'end' should be the total audio duration
|
| 340 |
+
df.loc[df.index[-1], 'end'] = total_duration
|
| 341 |
+
|
| 342 |
+
# Create a new 'duration' column based on the difference between 'start' and 'end'
|
| 343 |
+
df['duration'] = df['end'] - df['start']
|
| 344 |
+
|
| 345 |
+
# Save the updated DataFrame back to CSV, extracting filename and prepending 'updated_'
|
| 346 |
+
updated_csv_file = 'updated_' + csv_file.split('/')[-1] # Extract filename and prepend 'updated_'
|
| 347 |
+
df.to_csv(updated_csv_file, index=False)
|
| 348 |
+
|
| 349 |
+
print(f"Updated DataFrame saved to: {updated_csv_file}")
|
| 350 |
+
return updated_csv_file
|
| 351 |
+
|
| 352 |
+
# Example usage
|
| 353 |
+
csv_file = '/content/filtered_transcription.csv'
|
| 354 |
+
audio_file = musicownpath
|
| 355 |
+
update_dataframe_with_audio_duration(csv_file, audio_file)
|
| 356 |
+
|
| 357 |
+
"""### **Giphy Gif Download**"""
|
| 358 |
+
|
| 359 |
+
# prompt: write a code for "/content/dropped_2024-08-21_18-58-34.csv" to use Word column search in giphy api (API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7") search and download in /content/memes this folder name as the word name
|
| 360 |
+
|
| 361 |
+
import requests
|
| 362 |
+
import csv
|
| 363 |
+
import os
|
| 364 |
+
|
| 365 |
+
# Giphy API details
|
| 366 |
+
API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7"
|
| 367 |
+
SEARCH_URL = "https://api.giphy.com/v1/gifs/search"
|
| 368 |
+
|
| 369 |
+
# CSV and download directory
|
| 370 |
+
CSV_FILE = "/content/updated_filtered_transcription.csv"
|
| 371 |
+
DOWNLOAD_DIR = '/content/memes2'
|
| 372 |
+
|
| 373 |
+
# Create download directory if it doesn't exist
|
| 374 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 375 |
+
|
| 376 |
+
def download_giphy_gif(search_term, filename):
|
| 377 |
+
"""Downloads a GIF from Giphy based on the search term."""
|
| 378 |
+
params = {
|
| 379 |
+
'api_key': API_KEY,
|
| 380 |
+
'q': search_term,
|
| 381 |
+
'limit': 1
|
| 382 |
+
}
|
| 383 |
+
response = requests.get(SEARCH_URL, params=params)
|
| 384 |
+
data = response.json()
|
| 385 |
+
|
| 386 |
+
if data['data']:
|
| 387 |
+
gif_url = data['data'][0]['images']['original']['url']
|
| 388 |
+
gif_response = requests.get(gif_url)
|
| 389 |
+
|
| 390 |
+
with open(os.path.join(DOWNLOAD_DIR, filename), 'wb') as f:
|
| 391 |
+
f.write(gif_response.content)
|
| 392 |
+
print(f"Downloaded GIF for '{search_term}' as '{filename}'")
|
| 393 |
+
else:
|
| 394 |
+
print(f"No GIF found for '{search_term}'")
|
| 395 |
+
|
| 396 |
+
# Process the CSV file
|
| 397 |
+
with open(CSV_FILE, 'r', encoding='utf-8') as file:
|
| 398 |
+
reader = csv.DictReader(file)
|
| 399 |
+
for row in reader:
|
| 400 |
+
word = row['word']
|
| 401 |
+
filename = f"{word}.gif"
|
| 402 |
+
download_giphy_gif(word, filename)
|
| 403 |
+
|
| 404 |
+
"""### Updated Download gif
|
| 405 |
+
|
| 406 |
+
import requests
|
| 407 |
+
import csv
|
| 408 |
+
import os
|
| 409 |
+
|
| 410 |
+
# Giphy API details
|
| 411 |
+
API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7"
|
| 412 |
+
SEARCH_URL = "https://api.giphy.com/v1/gifs/search"
|
| 413 |
+
|
| 414 |
+
# CSV and download directory
|
| 415 |
+
CSV_FILE = "/content/updated_filtered_transcription.csv"
|
| 416 |
+
DOWNLOAD_DIR = '/content/memes2'
|
| 417 |
+
|
| 418 |
+
# Create download directory if it doesn't exist
|
| 419 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 420 |
+
|
| 421 |
+
def download_giphy_gif(search_term, index, offset=0):
|
| 422 |
+
Downloads a GIF from Giphy based on the search term.
|
| 423 |
+
params = {
|
| 424 |
+
'api_key': API_KEY,
|
| 425 |
+
'q': search_term,
|
| 426 |
+
'limit': 1,
|
| 427 |
+
'offset': offset
|
| 428 |
+
}
|
| 429 |
+
response = requests.get(SEARCH_URL, params=params)
|
| 430 |
+
data = response.json()
|
| 431 |
+
|
| 432 |
+
if data['data']:
|
| 433 |
+
gif_url = data['data'][0]['images']['original']['url']
|
| 434 |
+
gif_response = requests.get(gif_url)
|
| 435 |
+
|
| 436 |
+
filename = f"{index}.gif"
|
| 437 |
+
filepath = os.path.join(DOWNLOAD_DIR, filename)
|
| 438 |
+
|
| 439 |
+
with open(filepath, 'wb') as f:
|
| 440 |
+
f.write(gif_response.content)
|
| 441 |
+
print(f"Downloaded GIF for '{search_term}' as '{filename}'")
|
| 442 |
+
return gif_url
|
| 443 |
+
else:
|
| 444 |
+
print(f"No GIF found for '{search_term}'")
|
| 445 |
+
return None
|
| 446 |
+
|
| 447 |
+
# Process the CSV file
|
| 448 |
+
with open(CSV_FILE, 'r', encoding='utf-8') as file:
|
| 449 |
+
reader = csv.DictReader(file)
|
| 450 |
+
word_counts = {}
|
| 451 |
+
for index, row in enumerate(reader):
|
| 452 |
+
word = row['word']
|
| 453 |
+
if word in word_counts:
|
| 454 |
+
word_counts[word] += 1
|
| 455 |
+
else:
|
| 456 |
+
word_counts[word] = 1
|
| 457 |
+
|
| 458 |
+
gif_url = download_giphy_gif(word, index, offset=word_counts[word] - 1)
|
| 459 |
+
if gif_url:
|
| 460 |
+
row['link'] = gif_url
|
| 461 |
+
|
| 462 |
+
# Write the updated CSV file
|
| 463 |
+
with open(CSV_FILE, 'w', newline='', encoding='utf-8') as file:
|
| 464 |
+
writer = csv.DictWriter(file, fieldnames=['word', 'link'])
|
| 465 |
+
writer.writeheader()
|
| 466 |
+
writer.writerows([{'word': word, 'link': gif_url} for word, gif_url in [(row['word'], row['link']) for row in reader]])
|
| 467 |
+
"""
|
| 468 |
+
|
| 469 |
+
# prompt: write a code to save /content/updated_filtered_transcription.csv with index
|
| 470 |
+
"""
|
| 471 |
+
import pandas as pd
|
| 472 |
+
df = pd.read_csv('/content/updated_filtered_transcription.csv')
|
| 473 |
+
df.to_csv('/content/updated_filtered_transcription.csv', index=True)
|
| 474 |
+
"""
|
| 475 |
+
|
| 476 |
+
"""import requests
|
| 477 |
+
import csv
|
| 478 |
+
import os
|
| 479 |
+
|
| 480 |
+
# Giphy API details
|
| 481 |
+
API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7"
|
| 482 |
+
SEARCH_URL = "https://api.giphy.com/v1/gifs/search"
|
| 483 |
+
|
| 484 |
+
# CSV and download directory
|
| 485 |
+
CSV_FILE = "/content/updated_filtered_transcription.csv"
|
| 486 |
+
DOWNLOAD_DIR = '/content/memes3'
|
| 487 |
+
|
| 488 |
+
# Create download directory if it doesn't exist
|
| 489 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 490 |
+
|
| 491 |
+
def download_giphy_gif(search_term, index):
|
| 492 |
+
Downloads a GIF from Giphy based on the search term.
|
| 493 |
+
params = {
|
| 494 |
+
'api_key': API_KEY,
|
| 495 |
+
'q': search_term,
|
| 496 |
+
'limit': 1
|
| 497 |
+
}
|
| 498 |
+
response = requests.get(SEARCH_URL, params=params)
|
| 499 |
+
data = response.json()
|
| 500 |
+
|
| 501 |
+
if data['data']:
|
| 502 |
+
gif_url = data['data'][0]['images']['original']['url']
|
| 503 |
+
gif_response = requests.get(gif_url)
|
| 504 |
+
|
| 505 |
+
filename = f"{index}.gif"
|
| 506 |
+
with open(os.path.join(DOWNLOAD_DIR, filename), 'wb') as f:
|
| 507 |
+
f.write(gif_response.content)
|
| 508 |
+
print(f"Downloaded GIF for '{search_term}' as '{filename}'")
|
| 509 |
+
else:
|
| 510 |
+
print(f"No GIF found for '{search_term}'")
|
| 511 |
+
|
| 512 |
+
# Process the CSV file
|
| 513 |
+
with open(CSV_FILE, 'r', encoding='utf-8') as file:
|
| 514 |
+
reader = csv.DictReader(file)
|
| 515 |
+
for index, row in enumerate(reader):
|
| 516 |
+
word = row['word']
|
| 517 |
+
download_giphy_gif(word, index)
|
| 518 |
+
|
| 519 |
+
import requests
|
| 520 |
+
import csv
|
| 521 |
+
import os
|
| 522 |
+
|
| 523 |
+
# Giphy API details
|
| 524 |
+
API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7"
|
| 525 |
+
SEARCH_URL = "https://api.giphy.com/v1/gifs/search"
|
| 526 |
+
|
| 527 |
+
# CSV and download directory
|
| 528 |
+
CSV_FILE = "/content/updated_filtered_transcription.csv"
|
| 529 |
+
DOWNLOAD_DIR = '/content/memes3'
|
| 530 |
+
|
| 531 |
+
# Create download directory if it doesn't exist
|
| 532 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 533 |
+
|
| 534 |
+
def download_giphy_gif(search_term, index):
|
| 535 |
+
Downloads a GIF from Giphy based on the search term.
|
| 536 |
+
params = {
|
| 537 |
+
'api_key': API_KEY,
|
| 538 |
+
'q': search_term,
|
| 539 |
+
'limit': 1
|
| 540 |
+
}
|
| 541 |
+
response = requests.get(SEARCH_URL, params=params)
|
| 542 |
+
data = response.json()
|
| 543 |
+
|
| 544 |
+
if data['data']:
|
| 545 |
+
gif_url = data['data'][0]['images']['original']['url']
|
| 546 |
+
gif_response = requests.get(gif_url)
|
| 547 |
+
|
| 548 |
+
filename = f"{index}.gif"
|
| 549 |
+
filepath = os.path.join(DOWNLOAD_DIR, filename)
|
| 550 |
+
|
| 551 |
+
# Check if the file already exists
|
| 552 |
+
if not os.path.exists(filepath):
|
| 553 |
+
with open(filepath, 'wb') as f:
|
| 554 |
+
f.write(gif_response.content)
|
| 555 |
+
print(f"Downloaded GIF for '{search_term}' as '{filename}'")
|
| 556 |
+
else:
|
| 557 |
+
print(f"GIF for '{search_term}' already exists")
|
| 558 |
+
|
| 559 |
+
return gif_url
|
| 560 |
+
else:
|
| 561 |
+
print(f"No GIF found for '{search_term}'")
|
| 562 |
+
return None
|
| 563 |
+
|
| 564 |
+
# Process the CSV file
|
| 565 |
+
with open(CSV_FILE, 'r', encoding='utf-8') as file:
|
| 566 |
+
reader = csv.DictReader(file)
|
| 567 |
+
rows = list(reader) # Convert reader to a list to ensure it's fully read
|
| 568 |
+
|
| 569 |
+
# Check if any rows were read
|
| 570 |
+
if rows:
|
| 571 |
+
# Add a "link" column if it doesn't exist
|
| 572 |
+
if 'link' not in rows[0].keys():
|
| 573 |
+
for row in rows:
|
| 574 |
+
row['link'] = ''
|
| 575 |
+
|
| 576 |
+
# Download GIFs and update the "link" column
|
| 577 |
+
for index, row in enumerate(rows):
|
| 578 |
+
word = row['word']
|
| 579 |
+
gif_url = download_giphy_gif(word, index)
|
| 580 |
+
if gif_url:
|
| 581 |
+
row['link'] = gif_url
|
| 582 |
+
|
| 583 |
+
# Write the updated CSV file
|
| 584 |
+
with open(CSV_FILE, 'w', newline='', encoding='utf-8') as file:
|
| 585 |
+
writer = csv.DictWriter(file, fieldnames=rows[0].keys())
|
| 586 |
+
writer.writeheader()
|
| 587 |
+
writer.writerows(rows)
|
| 588 |
+
else:
|
| 589 |
+
print("The CSV file is empty.")
|
| 590 |
+
|
| 591 |
+
import os
|
| 592 |
+
import pandas as pd
|
| 593 |
+
|
| 594 |
+
# Read the CSV file
|
| 595 |
+
df = pd.read_csv('/content/updated_filtered_transcription.csv')
|
| 596 |
+
|
| 597 |
+
# Get the list of words from the DataFrame
|
| 598 |
+
words = df['word'].tolist()
|
| 599 |
+
|
| 600 |
+
# Get the list of GIF files in the directory
|
| 601 |
+
gif_files = [f for f in os.listdir('/content/memes2') if f.endswith('.gif')]
|
| 602 |
+
|
| 603 |
+
# Ensure we have enough words for the GIF files
|
| 604 |
+
if len(words) < len(gif_files):
|
| 605 |
+
raise ValueError("Not enough words in the CSV file to rename all GIFs.")
|
| 606 |
+
|
| 607 |
+
# Iterate through the GIF files and rename them
|
| 608 |
+
for i, gif_file in enumerate(gif_files):
|
| 609 |
+
try:
|
| 610 |
+
# Generate new filename based on index from the CSV
|
| 611 |
+
new_filename = f"{i+1}.gif" # Index starts from 1
|
| 612 |
+
|
| 613 |
+
# Construct the full paths for the old and new filenames
|
| 614 |
+
old_path = os.path.join('/content/memes2', gif_file)
|
| 615 |
+
new_path = os.path.join('/content/memes2', new_filename)
|
| 616 |
+
|
| 617 |
+
# Rename the file
|
| 618 |
+
os.rename(old_path, new_path)
|
| 619 |
+
print(f"Renamed '{gif_file}' to '{new_filename}'")
|
| 620 |
+
except Exception as e:
|
| 621 |
+
print(f"Error processing file '{gif_file}': {e}")
|
| 622 |
+
|
| 623 |
+
### Concate Gif (incomplete)
|
| 624 |
+
"""
|
| 625 |
+
|
| 626 |
+
# prompt: drop Unnamed: 0 and save the file
|
| 627 |
+
|
| 628 |
+
#import pandas as pd
|
| 629 |
+
#df = pd.read_csv('/content/updated_filtered_transcription.csv')
|
| 630 |
+
#df = df.drop(columns=['Unnamed: 0'])
|
| 631 |
+
#df.to_csv('/content/updated_filtered_transcription.csv', index=False)
|
| 632 |
+
df.head()
|
| 633 |
+
|
| 634 |
+
import moviepy.editor as mpe
|
| 635 |
+
import os
|
| 636 |
+
import csv
|
| 637 |
+
|
| 638 |
+
# CSV and download directory paths
|
| 639 |
+
CSV_FILE = '/content/updated_filtered_transcription.csv'
|
| 640 |
+
DOWNLOAD_DIR = '/content/memes2'
|
| 641 |
+
OUTPUT_VIDEO = 'updated_concatenated_memes.mp4'
|
| 642 |
+
|
| 643 |
+
# Get the GIF order and durations from the CSV file
|
| 644 |
+
gif_order = []
|
| 645 |
+
durations = {}
|
| 646 |
+
with open(CSV_FILE, 'r', encoding='utf-8') as file:
|
| 647 |
+
reader = csv.DictReader(file)
|
| 648 |
+
for row in reader:
|
| 649 |
+
gif_filename = row['word'] + '.gif'
|
| 650 |
+
duration = float(row['duration']) # Ensure this matches the column name in your CSV
|
| 651 |
+
gif_order.append(gif_filename)
|
| 652 |
+
durations[gif_filename] = duration
|
| 653 |
+
|
| 654 |
+
# Load, crop, and concatenate GIFs
|
| 655 |
+
clips = []
|
| 656 |
+
for gif_filename in gif_order:
|
| 657 |
+
gif_path = os.path.join(DOWNLOAD_DIR, gif_filename)
|
| 658 |
+
if os.path.exists(gif_path):
|
| 659 |
+
clip = mpe.VideoFileClip(gif_path).resize(height=480) # Resize to the same height
|
| 660 |
+
clip = clip.set_fps(24) # Match the frame rate for consistency
|
| 661 |
+
|
| 662 |
+
# Crop each GIF to the specified duration from the new CSV
|
| 663 |
+
max_duration = durations.get(gif_filename, clip.duration) # Use the duration from the CSV or the full clip duration if not found
|
| 664 |
+
if clip.duration > max_duration:
|
| 665 |
+
clip = clip.subclip(0, max_duration) # Keep up to the specified duration
|
| 666 |
+
|
| 667 |
+
clips.append(clip)
|
| 668 |
+
else:
|
| 669 |
+
print(f"Warning: GIF not found: {gif_filename}")
|
| 670 |
+
|
| 671 |
+
# Concatenate and save the video
|
| 672 |
+
if clips:
|
| 673 |
+
final_clip = mpe.concatenate_videoclips(clips, method="compose")
|
| 674 |
+
final_clip.write_videofile(OUTPUT_VIDEO, fps=24) # Set fps to match the GIFs
|
| 675 |
+
print(f"Concatenated video saved as {OUTPUT_VIDEO}")
|
| 676 |
+
else:
|
| 677 |
+
print("No GIFs found to concatenate.")
|
| 678 |
+
|
| 679 |
+
"""### concate_audio_gif_music"""
|
| 680 |
+
|
| 681 |
+
import moviepy.editor as mpe
|
| 682 |
+
import os
|
| 683 |
+
|
| 684 |
+
# File paths
|
| 685 |
+
video_file = '/content/updated_concatenated_memes.mp4'
|
| 686 |
+
music_file = musicownpath
|
| 687 |
+
audio_file = "/content/audio_output.mp3"
|
| 688 |
+
output_file = '/content/final_output.mp4'
|
| 689 |
+
|
| 690 |
+
# Load the video, music, and audio files
|
| 691 |
+
video_clip = mpe.VideoFileClip(video_file)
|
| 692 |
+
music_clip = mpe.VideoFileClip(music_file)
|
| 693 |
+
audio_clip = mpe.AudioFileClip(audio_file)
|
| 694 |
+
|
| 695 |
+
# Duration of the video
|
| 696 |
+
video_duration = video_clip.duration
|
| 697 |
+
|
| 698 |
+
# Ensure the music duration matches the video duration
|
| 699 |
+
if music_clip.duration < video_duration:
|
| 700 |
+
# Repeat the music to match the video duration
|
| 701 |
+
n_repeats = int(video_duration // music_clip.duration) + 1
|
| 702 |
+
music_clip = mpe.concatenate_videoclips([music_clip] * n_repeats).subclip(0, video_duration)
|
| 703 |
+
elif music_clip.duration > video_duration:
|
| 704 |
+
music_clip = music_clip.subclip(0, video_duration)
|
| 705 |
+
|
| 706 |
+
# Adjust music volume to 50% and keep audio volume at 100%
|
| 707 |
+
music_clip = music_clip.volumex(0.3) # Reduce music volume to 50%
|
| 708 |
+
|
| 709 |
+
# Ensure the audio duration matches the video duration
|
| 710 |
+
if audio_clip.duration < video_duration:
|
| 711 |
+
# Repeat the audio to match the video duration
|
| 712 |
+
n_repeats = int(video_duration // audio_clip.duration) + 1
|
| 713 |
+
audio_clip = mpe.concatenate_audioclips([audio_clip] * n_repeats).subclip(0, video_duration)
|
| 714 |
+
elif audio_clip.duration > video_duration:
|
| 715 |
+
audio_clip = audio_clip.subclip(0, video_duration)
|
| 716 |
+
|
| 717 |
+
# Set the audio of the video clip to the adjusted audio
|
| 718 |
+
video_clip = video_clip.set_audio(audio_clip)
|
| 719 |
+
|
| 720 |
+
# Write the final output video with the adjusted music and audio
|
| 721 |
+
final_clip = video_clip.set_audio(music_clip.audio)
|
| 722 |
+
final_clip.write_videofile(output_file, codec='libx264', audio_codec='aac')
|
| 723 |
+
|
| 724 |
+
print(f"Final video saved as {output_file}")
|
| 725 |
+
|
| 726 |
+
import moviepy.editor as mpe
|
| 727 |
+
import os
|
| 728 |
+
|
| 729 |
+
# File paths
|
| 730 |
+
video_file = '/content/updated_concatenated_memes.mp4'
|
| 731 |
+
music_file = musicownpath
|
| 732 |
+
audio_file = "/content/audio_output.mp3"
|
| 733 |
+
output_file = '/content/final_output2.mp4'
|
| 734 |
+
|
| 735 |
+
# Load the video, music, and audio files
|
| 736 |
+
video_clip = mpe.VideoFileClip(video_file)
|
| 737 |
+
music_clip = mpe.VideoFileClip(music_file)
|
| 738 |
+
audio_clip = mpe.AudioFileClip(audio_file)
|
| 739 |
+
|
| 740 |
+
# Duration of the video
|
| 741 |
+
video_duration = video_clip.duration
|
| 742 |
+
|
| 743 |
+
# Ensure the music duration matches the video duration
|
| 744 |
+
if music_clip.duration < video_duration:
|
| 745 |
+
# Repeat the music to match the video duration
|
| 746 |
+
n_repeats = int(video_duration // music_clip.duration) + 1
|
| 747 |
+
music_clip = mpe.concatenate_videoclips([music_clip] * n_repeats).subclip(0, video_duration)
|
| 748 |
+
elif music_clip.duration > video_duration:
|
| 749 |
+
music_clip = music_clip.subclip(0, video_duration)
|
| 750 |
+
|
| 751 |
+
# Ensure the audio duration matches the video duration
|
| 752 |
+
if audio_clip.duration < video_duration:
|
| 753 |
+
# Repeat the audio to match the video duration
|
| 754 |
+
n_repeats = int(video_duration // audio_clip.duration) + 1
|
| 755 |
+
audio_clip = mpe.concatenate_audioclips([audio_clip] * n_repeats).subclip(0, video_duration)
|
| 756 |
+
elif audio_clip.duration > video_duration:
|
| 757 |
+
audio_clip = audio_clip.subclip(0, video_duration)
|
| 758 |
+
|
| 759 |
+
# Adjust music volume to 50% and keep audio volume at 100%
|
| 760 |
+
music_clip = music_clip.volumex(0.2) # Reduce music volume to 50%
|
| 761 |
+
|
| 762 |
+
# Set the audio of the video clip to the adjusted audio
|
| 763 |
+
video_clip = video_clip.set_audio(audio_clip)
|
| 764 |
+
|
| 765 |
+
# Combine the video with adjusted music
|
| 766 |
+
final_audio = mpe.CompositeAudioClip([music_clip.audio, audio_clip])
|
| 767 |
+
final_clip = video_clip.set_audio(final_audio)
|
| 768 |
+
|
| 769 |
+
# Write the final output video
|
| 770 |
+
final_clip.write_videofile(output_file, codec='libx264', audio_codec='aac')
|
| 771 |
+
|
| 772 |
+
print(f"Final video saved as {output_file}")
|