-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.py
More file actions
51 lines (40 loc) · 1.6 KB
/
code.py
File metadata and controls
51 lines (40 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import ffmpeg
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
os.remove("temp.png")
# keep track of videos processed
print("Processed", idx + 1, "videos")