2024-05-16 15:43:12 -04:00
|
|
|
import subprocess
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import datetime
|
|
|
|
|
import time
|
2024-05-29 13:48:48 -04:00
|
|
|
import calendar
|
2024-05-16 15:43:12 -04:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
from sortedcontainers import SortedDict
|
|
|
|
|
import tqdm
|
2024-05-25 03:11:38 -04:00
|
|
|
from PIL import Image
|
2024-05-16 15:43:12 -04:00
|
|
|
|
2024-07-05 10:24:41 -04:00
|
|
|
path_to_images = r"..\composite\goes18"
|
|
|
|
|
output_file = r"..\goes18_2023.mp4"
|
|
|
|
|
interp_file = r"..\goes18_2023_interp.mp4"
|
2024-05-18 19:05:21 -04:00
|
|
|
ffmpeg_path = r"..\ffmpeg.exe"
|
2024-07-05 10:24:41 -04:00
|
|
|
starttime = calendar.timegm(datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc).timetuple())
|
|
|
|
|
stoptime = calendar.timegm(datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc).timetuple())
|
2024-05-25 03:11:38 -04:00
|
|
|
min_file_size = 350000 # Detect and remove corrupted files by filtering by file size
|
|
|
|
|
max_file_size = 450000
|
|
|
|
|
encoding_crf = 16
|
2024-06-04 17:17:06 -04:00
|
|
|
max_frame_interp = 120
|
2024-05-25 03:11:38 -04:00
|
|
|
|
2024-06-04 17:17:06 -04:00
|
|
|
# Set up ffmpeg to stream images from an input pipe
|
|
|
|
|
command_line = f'{ffmpeg_path} -y -f image2pipe -framerate 60 -i - -c:v libx264 -crf {encoding_crf} -preset veryfast {output_file}'
|
|
|
|
|
print(command_line)
|
|
|
|
|
p = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
2024-05-16 15:43:12 -04:00
|
|
|
|
2024-05-25 03:11:38 -04:00
|
|
|
black_image = Image.fromarray(np.zeros((1080, 1920, 3), dtype='uint8'))
|
2024-05-16 15:43:12 -04:00
|
|
|
|
|
|
|
|
files_by_time = SortedDict()
|
|
|
|
|
for root, dirs, files in tqdm.tqdm(os.walk(path_to_images), desc="Finding and filtering files."):
|
|
|
|
|
for f in files:
|
|
|
|
|
if f.endswith('.jpg'):
|
|
|
|
|
fpath = os.path.join(root, f)
|
|
|
|
|
fsize = os.path.getsize(fpath)
|
2024-05-25 03:11:38 -04:00
|
|
|
time_chunk = f.replace('-', '.').replace('_', '.').split('.')[1]
|
|
|
|
|
ftime = int(time_chunk)
|
2024-05-16 15:43:12 -04:00
|
|
|
if (fsize > min_file_size) and (fsize < max_file_size) and (ftime > starttime) and (ftime < stoptime):
|
|
|
|
|
files_by_time[ftime] = fpath
|
|
|
|
|
|
|
|
|
|
difftimes = np.diff(files_by_time.keys())
|
|
|
|
|
unique, counts = np.unique(difftimes, return_counts=True)
|
|
|
|
|
interval = unique[0]
|
|
|
|
|
assert np.sum((unique % interval) > 0) == 0 # Ensure all our timestamps align perfectly with our interval
|
|
|
|
|
|
|
|
|
|
prevtime = files_by_time.peekitem(0)[0] - interval
|
2024-06-04 17:17:06 -04:00
|
|
|
for t, f in tqdm.tqdm(files_by_time.items(), desc="FFMPEG encoding images", total = len(files_by_time)):
|
2024-05-16 15:43:12 -04:00
|
|
|
framejump = (t - prevtime) // interval
|
2024-05-25 03:11:38 -04:00
|
|
|
if framejump < max_frame_interp:
|
|
|
|
|
for i in range(framejump - 1): # Fill black frames to fill gaps, these will later be interpolated with ffmpeg
|
2024-06-04 17:17:06 -04:00
|
|
|
black_image.save(p.stdin, 'jpeg', quality = 95)
|
2024-05-16 15:43:12 -04:00
|
|
|
else: # Skip past intervals that are too large to fill reasonably
|
|
|
|
|
print(f"Detected a frame gap of: {framejump}!")
|
2024-06-04 17:17:06 -04:00
|
|
|
with open(f, 'rb') as fh:
|
|
|
|
|
p.stdin.write(fh.read())
|
2024-05-16 15:43:12 -04:00
|
|
|
prevtime = t
|
|
|
|
|
|
2024-06-04 17:17:06 -04:00
|
|
|
p.stdin.close() # Close the ffmpeg input pipe
|
|
|
|
|
p.wait() # Wait for ffmpeg to finish encoding
|
2024-05-25 03:11:38 -04:00
|
|
|
|
2024-06-04 17:17:06 -04:00
|
|
|
command_line = f'{ffmpeg_path} -y -i {output_file} -vf blackframe=0,metadata=select:key=lavfi.blackframe.pblack:value=95:function=less,minterpolate=mi_mode=mci:mc_mode=aobmc:me_mode=bidir:vsbmc=1:me=fss -c:a copy -c:v libx264 -crf {encoding_crf} -preset veryfast {interp_file}'
|
2024-05-25 03:11:38 -04:00
|
|
|
print(command_line)
|
|
|
|
|
|
|
|
|
|
pipe = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE).stdout
|
|
|
|
|
output = pipe.read().decode()
|
|
|
|
|
pipe.close()
|