From 77d26dabe8f62ac6a32458e607fedb2cf24779a6 Mon Sep 17 00:00:00 2001 From: bharath <33729709+bharath5673@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:07:21 +0530 Subject: [PATCH 1/3] Update code.py --- code.py | 92 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/code.py b/code.py index 9b23a8e..66217b1 100644 --- a/code.py +++ b/code.py @@ -1,51 +1,55 @@ import os -import ffmpeg +import subprocess from PIL import Image from os.path import join from PIL.Image import Resampling -direct = "videos" -output = "output" - -# make output directory if not already created -os.makedirs("output", exist_ok = True) - -# retrieve all files from the input video folder -files = [] -for (_, _, filenames) in os.walk(direct): - files.extend(filenames) -files = [f for f in files if f.endswith(".mp4")] - -for (idx, fi) in enumerate(files): - - # designate input and output file names - file_name = join(direct, fi) - fout = join(output, fi) - - # checking video dimensions - probe = ffmpeg.probe(file_name) - video_streams = [stream for stream in probe["streams"] if stream["codec_type"] == "video"] - base_width = video_streams[0]['width'] - 20 - - # resizing overlay to fit - im = Image.open('overlay.png') - width, height = im.size - ratio = base_width/width - im = im.resize((base_width, int(ratio * height)), Resampling.LANCZOS) - im.save("temp.png") - - # get video and audio streams - vid_stream = ffmpeg.input(file_name).video - audio_stream = ffmpeg.input(file_name).audio - vid_background = ffmpeg.input('temp.png') - - # overlay background on video stream and mix in original audio stream - overlaid_vid_stream = ffmpeg.overlay(vid_stream, vid_background, x = 10, y = 10) - output_video_and_audio = ffmpeg.output(audio_stream, overlaid_vid_stream, fout) - output_video_and_audio.overwrite_output().run(quiet = False) # set quiet = True to remove output clutter - - # clean up +# Input and output directories +input_dir = "videos" +output_dir = "output" + +# Create the output directory if it doesn't exist +os.makedirs(output_dir, exist_ok=True) + +# Retrieve all .mp4 files from the input video folder +files = [f for f in os.listdir(input_dir) if f.endswith(".mp4")] + +for idx, file in enumerate(files): + input_file = join(input_dir, file) + output_file = join(output_dir, file) + + # Check video dimensions using subprocess with ffprobe + cmd = [ + "ffprobe", "-v", "error", + "-select_streams", "v:0", + "-show_entries", "stream=width,height", + "-of", "csv=p=0:s=x", + input_file + ] + result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + width, height = map(int, result.stdout.strip().split('x')) + + # Adjust the width for overlay + base_width = width - 20 # Adjusted width for overlay + + # Resize overlay image to fit video + overlay_image = Image.open('overlay.png') + overlay_ratio = base_width / overlay_image.width + overlay_resized = overlay_image.resize((base_width, int(overlay_image.height * overlay_ratio)), Resampling.LANCZOS) + overlay_resized.save("temp.png") + + # Overlay video with FFmpeg + cmd = [ + "ffmpeg", "-i", input_file, + "-i", "temp.png", + "-filter_complex", f"[0:v][1:v] overlay=10:10,scale={width*2}:-1", + "-c:a", "copy", + "-y", output_file + ] + subprocess.run(cmd) + + # Clean up temporary overlay file os.remove("temp.png") - # keep track of videos processed - print("Processed", idx + 1, "videos") \ No newline at end of file + # Display progress + print(f"Processed {idx + 1}/{len(files)} videos: {file}") From 5889965270abd42c5b41d4ccf212e6fd978f4a50 Mon Sep 17 00:00:00 2001 From: bharath <33729709+bharath5673@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:12:11 +0530 Subject: [PATCH 2/3] Create README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c8dc635 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ + +```bash +conda install ffmpeg +pip install ffmpeg + +``` + +#### ffmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0) From aa50e7f819ab4866d4a70a2724b8866b76e0a35e Mon Sep 17 00:00:00 2001 From: bharath <33729709+bharath5673@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:12:36 +0530 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c8dc635..2e318b4 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,5 @@ conda install ffmpeg pip install ffmpeg ``` - +#### python3.10 #### ffmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0)