Last active
May 28, 2022 01:21
-
-
Save spolo96/0bd57e26f264909da9d34117e8be88a6 to your computer and use it in GitHub Desktop.
A script to automatically compress videos to a desired target size and then concatenates all the compressed videos into a single one in order to properly share it to Youtube, Instagram, Facebook etc.
This file contains 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
from natsort import natsorted | |
import os | |
import glob | |
import ffmpeg # Remember to install ffmpeg in your PC! | |
from datetime import date, timedelta | |
from tqdm import tqdm | |
from moviepy.editor import * | |
VIDEO_PATH = "M:\Videos\Halo The Master Chief Collection" # Path of Desired Videos | |
os.chdir(VIDEO_PATH) # Change path | |
today = date.today() | |
# Get Date in NVidia GeForce Experience Format (Year.Month.Day) E.g: 2022.04.03 | |
pattern = today.strftime("%Y.%m.%d") | |
# Get Today Videos | |
today_videos = glob.glob("*"+pattern+"*.mp4") | |
# Function by legendary Woa as found here: https://stackoverflow.com/questions/64430805/how-to-compress-video-to-target-size-by-python | |
#compress_video(video_path, output_video_name, desired video size in MB E.g (80 * 1000) means we want 80 MB as target size) | |
def compress_video(video_full_path, output_file_name, target_size): | |
# Reference: https://en.wikipedia.org/wiki/Bit_rate#Encoding_bit_rate | |
# Target Size Assumed to be in KB. | |
min_audio_bitrate = 32000 | |
max_audio_bitrate = 256000 | |
probe = ffmpeg.probe(video_full_path) | |
# Video duration, in s. | |
duration = float(probe['format']['duration']) | |
# Audio bitrate, in bps. | |
audio_bitrate = float(next((s for s in probe['streams'] if s['codec_type'] == 'audio'), None)['bit_rate']) | |
# Target total bitrate, in bps. | |
target_total_bitrate = (target_size * 1024 * 8) / (1.073741824 * duration) | |
# Target audio bitrate, in bps | |
if 10 * audio_bitrate > target_total_bitrate: | |
audio_bitrate = target_total_bitrate / 10 | |
if audio_bitrate < min_audio_bitrate < target_total_bitrate: | |
audio_bitrate = min_audio_bitrate | |
elif audio_bitrate > max_audio_bitrate: | |
audio_bitrate = max_audio_bitrate | |
# Target video bitrate, in bps. | |
video_bitrate = target_total_bitrate - audio_bitrate | |
i = ffmpeg.input(video_full_path) | |
ffmpeg.output(i, os.devnull, | |
**{'c:v': 'libx264', 'b:v': video_bitrate, 'pass': 1, 'f': 'mp4'} | |
).overwrite_output().run() | |
ffmpeg.output(i, output_file_name, | |
**{'c:v': 'libx264', 'b:v': video_bitrate, 'pass': 2, 'c:a': 'aac', 'b:a': audio_bitrate} | |
).overwrite_output().run() | |
for video in tqdm(today_videos): | |
compress_video(video, "compressed "+video, 80 * 1000) | |
compressed_videos = glob.glob("compressed*.mp4") # Get All Compressed Videos | |
compressed_videoclips = [VideoFileClip(i) for i in compressed_videos] # Compressed Videos to VideoFileClips to concatenate them | |
final_video = concatenate_videoclips(compressed_videoclips) | |
output_video_name = "Automatic "+today_videos[0].split(pattern)[0]+pattern+" videos" # Create Output Name to differentiate videos | |
#writing the video into a file / saving the combined video | |
final_video.write_videofile(output_video_name+".mp4") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment