Created
January 15, 2025 15:57
-
-
Save wcpaez/74b9b1470501f790ca2641007e1607a6 to your computer and use it in GitHub Desktop.
Generate video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url = "https://www.youtube.com/watch?v=7qZl_5xHoBw" | |
start_time = "01:05:45" | |
end_time = "01:08:00" | |
output_name = "pucp" | |
watermark = "logo.png" | |
watermark_position = "top-left" | |
fragment_output = "#{output_name}_fragment.mp4" | |
watermarked_video = "#{output_name}_watermarked.mp4" | |
def download_fragment(url, start_time, end_time, output) | |
if File.exist?(output) | |
puts "The fragment file #{output} already exists. Skipping download..." | |
return | |
end | |
puts "Downloading fragment from #{start_time} to #{end_time}..." | |
system("yt-dlp --download-sections \"*#{start_time}-#{end_time}\" --remux-video mp4 -o \"#{output}\" \"#{url}\"") | |
unless File.exist?(output) | |
puts "Error: Could not download the video fragment." | |
exit(1) | |
end | |
end | |
def get_overlay_position(position) | |
case position | |
when "top-left" | |
"10:10" | |
when "top-right" | |
"W-w-10:10" | |
when "bottom-left" | |
"10:H-h-10" | |
when "bottom-right" | |
"W-w-10:H-h-10" | |
else | |
"W-w-10:10" | |
end | |
end | |
def add_watermark(video_input, watermark, output, position) | |
puts "Adding watermark at position #{position}..." | |
overlay = get_overlay_position(position) | |
system("ffmpeg -i \"#{video_input}\" -i \"#{watermark}\" -filter_complex \"overlay=#{overlay}\" -c:v libx264 -c:a aac -strict experimental \"#{output}\"") | |
unless File.exist?(output) | |
puts "Error: Could not add the watermark." | |
exit(1) | |
end | |
end | |
puts "Starting process..." | |
download_fragment(url, start_time, end_time, fragment_output) | |
add_watermark(fragment_output, watermark, watermarked_video, watermark_position) | |
puts "Process completed. The video with the watermark has been saved as #{watermarked_video}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment