Created
June 23, 2023 15:28
-
-
Save JupyterJones/cdd1837274a1849863e76a4f7b3db1d3 to your computer and use it in GitHub Desktop.
creates a video from shuffled video files in a directory
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
import random | |
import glob | |
import subprocess | |
from PIL import Image | |
from datetime import datetime | |
def tubevid(DIR): | |
image_files = glob.glob(DIR + "*.jpg") | |
random.shuffle(image_files) | |
# Determine the dimensions of the first image | |
REF = image_files[0] | |
im = Image.open(REF) | |
h, w = im.size | |
# Prepare the border image | |
BORDER = "/home/jack/Desktop/HDD500/FLASK/static/assets/512x666Border.png" | |
bd = Image.open(BORDER) | |
bd = bd.resize((h, w), Image.BICUBIC) | |
BOR = "BOR.png" | |
bd.save(BOR) | |
VIDEOS = "EXPERIMENT/XXXX1.mp4" | |
current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | |
filename = f"EXPERIMENT/_file_new{current_datetime}.mp4" | |
silentFINAL = "silentTEMP.mp4" | |
# Create a text file to store the list of shuffled images | |
with open("image_list.txt", "w") as file: | |
for image_file in image_files: | |
file.write(f"file '{image_file}'\n") | |
# Concatenate the shuffled images into a single video using FFmpeg | |
ffmpeg_concat_command = f"ffmpeg -hide_banner -f concat -safe 0 -i image_list.txt -vf 'scale={h}:{w},setsar=1:1' -c:v libx264 -pix_fmt yuv420p -y {VIDEOS}" | |
subprocess.run(ffmpeg_concat_command, shell=True) | |
# Remove the temporary image list file | |
subprocess.run("rm image_list.txt", shell=True) | |
ffmpeg_mp4_command = f"ffmpeg -hide_banner -i {VIDEOS} -filter:v 'setpts=20*PTS,minterpolate=fps=25:scd=none:me_mode=bidir:vsbmc=1:search_param=200' -t 58 -y {filename}" | |
subprocess.run(ffmpeg_mp4_command, shell=True) | |
#Overlay filename with BORDER | |
ffmpeg_overlay_command = f"ffmpeg -i {filename} -i {BOR} -filter_complex overlay=0:0 -c:a copy -y {silentFINAL}" | |
subprocess.run(ffmpeg_overlay_command, shell=True) | |
MUS = "/mnt/HDD500/collections/Music/" | |
MUSIC = random.choice(glob.glob(MUS+"*.mp3")) | |
FINAL = f"/mnt/HDD500/FLASK/static/current_project/{current_datetime}setpoint20minterpolate.mp4" | |
random_number = randint(0,200) | |
ffmpeg_final_command = f"ffmpeg -ss 0 -i {silentFINAL} -ss {random_number} -i {MUSIC} -map 0:v -map 1:a -c:v copy -c:a aac -shortest -y {FINAL}" | |
subprocess.run(ffmpeg_final_command, shell=True) | |
return FINAL | |
DIR = "/home/jack/Desktop/HDD500/FLASK/static/milestones_resources/old-man_files/" | |
tubevid(DIR) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment