diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e318b4 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ + +```bash +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) 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}")