From a5649a3987ce6dec4bfce60d6eab4da72f284153 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Fri, 19 Dec 2025 13:47:23 +0100 Subject: [PATCH] added docker compose, and youtube url process --- .DS_Store | Bin 0 -> 6148 bytes .dockerignore | 46 ++++++++++++++++ Dockerfile | 48 +++++++++++++++++ README.md | 38 ++++++++++++-- docker-compose.yml | 19 +++++++ main.py | 113 ++++++++++++++++++++++++++++++++++++++-- requirements.txt | 1 + videos/.DS_Store | Bin 0 -> 6148 bytes videos/output/.DS_Store | Bin 0 -> 6148 bytes 9 files changed, 257 insertions(+), 8 deletions(-) create mode 100644 .DS_Store create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 videos/.DS_Store create mode 100644 videos/output/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7a79a903caf6c82e586d11d9c5c6e6533b7c9d2a GIT binary patch literal 6148 zcmeHKJ8r`;3?*A5Fp$M#M_r*e5CZoExj@i5LjeOGf^_!ObMyvrcx zTfS}3r~nn90#twsP=U=A$P(M`H$RyNQUNM(?+V!Wp}-Am;vDFo4h-G`02_qeF!x>p zSS$dniE|(#Fbyg&sG2Q?1|9K|c{Onk47zAGADTC7b|~t%*{e:"/\\|?*]', '', filename) + # Replace spaces with underscores + filename = filename.replace(' ', '_') + # Limit length + return filename[:100] + + +def download_youtube_video(url, output_dir="."): + """ + Downloads a YouTube video using yt-dlp. + Returns the path to the downloaded video and the video title. + """ + print("šŸ“„ Downloading video from YouTube...") + step_start_time = time.time() + + # First, get video info to determine the title + ydl_opts_info = { + 'quiet': True, + 'no_warnings': True, + } + + with yt_dlp.YoutubeDL(ydl_opts_info) as ydl: + info = ydl.extract_info(url, download=False) + video_title = info.get('title', 'youtube_video') + sanitized_title = sanitize_filename(video_title) + + # Download the video + # Force H.264 codec (avc1) instead of AV1 for better compatibility with OpenCV + output_template = os.path.join(output_dir, f'{sanitized_title}.%(ext)s') + + # Delete existing file if present (to force re-download with correct codec) + expected_file = os.path.join(output_dir, f'{sanitized_title}.mp4') + if os.path.exists(expected_file): + os.remove(expected_file) + print(f"šŸ—‘ļø Removed existing file to re-download with H.264 codec") + + ydl_opts = { + 'format': 'bestvideo[vcodec^=avc1][ext=mp4]+bestaudio[ext=m4a]/bestvideo[vcodec^=avc1]+bestaudio/best[ext=mp4]/best', + 'outtmpl': output_template, + 'merge_output_format': 'mp4', + 'quiet': False, + 'no_warnings': True, + 'overwrites': True, # Force overwrite if file exists + } + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + ydl.download([url]) + + # Find the downloaded file + downloaded_file = os.path.join(output_dir, f'{sanitized_title}.mp4') + + if not os.path.exists(downloaded_file): + # Try to find any file matching the pattern + for f in os.listdir(output_dir): + if f.startswith(sanitized_title) and f.endswith('.mp4'): + downloaded_file = os.path.join(output_dir, f) + break + + step_end_time = time.time() + print(f"āœ… Video downloaded in {step_end_time - step_start_time:.2f}s: {downloaded_file}") + + return downloaded_file, sanitized_title + + if __name__ == '__main__': parser = argparse.ArgumentParser(description="Smartly crops a horizontal video into a vertical one.") - parser.add_argument('-i', '--input', type=str, required=True, help="Path to the input video file.") - parser.add_argument('-o', '--output', type=str, required=True, help="Path to the output video file.") + + # Create mutually exclusive group for input source + input_group = parser.add_mutually_exclusive_group(required=True) + input_group.add_argument('-i', '--input', type=str, help="Path to the input video file.") + input_group.add_argument('-u', '--url', type=str, help="YouTube URL to download and process.") + + parser.add_argument('-o', '--output', type=str, help="Path to the output video file. (Auto-generated if not provided with --url)") + parser.add_argument('--keep-original', action='store_true', help="Keep the downloaded YouTube video after processing.") args = parser.parse_args() script_start_time = time.time() - - input_video = args.input - final_output_video = args.output + + downloaded_video = None # Track if we downloaded a video + + # Handle YouTube URL + if args.url: + # Determine output directory + output_dir = os.path.dirname(args.output) if args.output else "." + if output_dir == "": + output_dir = "." + os.makedirs(output_dir, exist_ok=True) + + # Download the video + input_video, video_title = download_youtube_video(args.url, output_dir) + downloaded_video = input_video + + # Generate output filename if not provided + if args.output: + final_output_video = args.output + else: + final_output_video = os.path.join(output_dir, f"{video_title}_vertical.mp4") + else: + if not args.output: + parser.error("--output is required when using --input") + input_video = args.input + final_output_video = args.output # Define temporary file paths based on the output name base_name = os.path.splitext(final_output_video)[0] @@ -288,6 +386,11 @@ if __name__ == '__main__': # Clean up temp files os.remove(temp_video_output) os.remove(temp_audio_output) + + # Clean up downloaded YouTube video if not keeping it + if downloaded_video and not args.keep_original: + os.remove(downloaded_video) + print(f"šŸ—‘ļø Cleaned up downloaded video: {downloaded_video}") script_end_time = time.time() print(f"\nšŸŽ‰ All done! Final video saved to {final_output_video}") diff --git a/requirements.txt b/requirements.txt index 15d3ec2..b944e78 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ ultralytics torch torchvision tqdm +yt-dlp diff --git a/videos/.DS_Store b/videos/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..db6a42faa4a522592971400c3f2b13c49a5b6150 GIT binary patch literal 6148 zcmeHK%}T>S5Z>*N-BN@e6nb3nTCm!p6fYsx7cim+m718M!I&*gVh^Q|v%Zi|;`2DO zyA|5%Nf0bEF#GM!&Lqs2WhcWJ<8B@vFxF&@2~fmR9fofN_fglRWG!VtuAh;G>3tk1 z6~mI`KQMsbuF4{ourbTo()Zh93QcAsn5N?(Rsp!b2je(OvrgxOm&%puR!!7IL)?0g zYU*WvmZz>ixyIE69E4yo_k+u5G#xf~PF0fmQ8Jq7fG8Y+%gt4kglg)lJPA`B>zNHv z7xiJYHJf#h`)%3n_U3Il>mPR7ve!MD&+B4$Z~x?c_!K`U>ecYg7iVvKh5t8LFXi;& zO_Eq84`5%!tc1{#7$63Sfz4#VUV5Ujndwnh!~iky3kL9hkf4Z;#loPzI$+Q(0AK~o zT40X71ja~Wj>W>D31{3kAKbo~+YW{6x5M+Lc4yo% zNFy;o3@kH%y&p&u>;Ku${l9jiffyhLHj@Ef8F&L1aH1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0