Created
January 20, 2021 20:58
-
-
Save kylemcdonald/94d5025c62a298e188ecb1124e3537e5 to your computer and use it in GitHub Desktop.
Make multithumbnails for Parler videos.
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
""" | |
This file reads a list of Parler video IDs and generates "multithumbnails" (a sequence of 9 evenly-spaced thumbnails). | |
The multithumbnails are stored in the multithumbnails/linear and multithumbnails/rect directories. | |
linear/ puts the thumbnails side-by-side in a linear sequence. | |
rect/ puts the thumbnails in a 3x3 rectangle. | |
To run, download this script from GitHub then: | |
git clone https://github.com/kylemcdonald/python-utils.git utils | |
sudo apt install python3 python3-pip | |
pip3 install --user --upgrade pip | |
pip3 install --user numpy ffmpeg-python opencv-python pillow | |
python3 make-multithumbnails.py id-list.txt | |
""" | |
import sys | |
import os | |
import time | |
from utils.ffmpeg import vidread | |
from utils.imutil import imresize | |
from utils.imutil import imwrite | |
from utils.mosaic import make_mosaic | |
from utils.list_files import list_files | |
from utils.list_files import get_stem | |
from utils.progress import progress | |
input_fn = sys.argv[1] | |
output_dir = 'multithumbnails/' | |
max_side = 320 | |
os.makedirs(os.path.join(output_dir, 'linear'), exist_ok=True) | |
os.makedirs(os.path.join(output_dir, 'rect'), exist_ok=True) | |
with open(input_fn) as f: | |
ids = f.read().splitlines() | |
for video_id in progress(ids): | |
linear_fn = os.path.join(output_dir, 'linear', video_id + '.jpg') | |
rect_fn = os.path.join(output_dir, 'rect', video_id + '.jpg') | |
if os.path.exists(linear_fn) and os.path.exists(rect_fn): | |
continue | |
fn = f'.cache/{video_id}' | |
os.system(f'aws s3 cp --request-payer requester s3://ddosecrets-parler/{video_id} {fn}') | |
thumbs = [] | |
samples = 9 | |
try: | |
for frame in vidread(fn, samples=samples): | |
thumb = imresize(frame, max_side=max_side) | |
thumbs.append(thumb) | |
except KeyboardInterrupt: | |
raise | |
except: | |
print('read/resize error', fn) | |
continue | |
if len(thumbs) > samples: | |
thumbs = thumbs[1:] | |
if len(thumbs) != samples: | |
print('incorrect total:', fn) | |
continue | |
print('='*30,'success','='*30) | |
imwrite(linear_fn, make_mosaic(thumbs, ny=1)) | |
imwrite(rect_fn, make_mosaic(thumbs)) | |
try: | |
os.remove(fn) | |
except FileNotFoundError: | |
print('ignoring deletion') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment