Skip to content

Commit

Permalink
new video snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed Jul 10, 2023
1 parent eefb110 commit fd93875
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
65 changes: 65 additions & 0 deletions snippets/ffmpeg/ffmpeg_encode_subfolder_sequences_to_prores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Encode sequences in subfolders to Prores

import re
from pathlib import Path
import subprocess

## Target directory containing sequence folders

paths = [
'/sequence_folder_to_encode_1',
'/sequence_folder_to_encode_2',
]

re_seq = re.compile(r'(.*?)_\d{4}')
re_num = re.compile(r'_(\d{4})\.')

from time import time

start = time()

for fp in paths:
fp = Path(fp)
print('\n\n---> ', fp)

for subfolder in fp.iterdir():
## iterate in subfolder
if not subfolder.is_dir():
continue
if not 'Smoke' in subfolder.name:
print(f'SKIP {subfolder.name}')
continue

file_list = [f for f in subfolder.iterdir() if f.is_file() and re_num.search(f.name)]
if len(file_list) < 2:
print(subfolder.name, 'skip, not enough images')
continue

# print(file_list[0])
## /!\ important ! Sort result before groupby (otherwise multiple groups with using same key)
file_list.sort(key=lambda x: int(re_num.search(x.name).group(1)))

first_file = file_list[0]

unpadded_stem = re_seq.search(first_file.name).group(1)

num = str(int(re_num.search(first_file.name).group(1)))
print(subfolder.name, ' with num digit: ', num)

cmd = [
'ffmpeg',
'-f', 'image2',
'-start_number', num,
'-i', str(first_file.with_stem(f'{unpadded_stem}_%04d')), # ex: '.../render/img_%04d.png',
'-r', '24', # 24 fps
'-c:v', 'prores_ks',
'-pix_fmt', 'yuva444p10le',
'-alpha_bits', '16',
'-profile:v', '4444',
str(fp / f'{unpadded_stem}.mov')
]

print(' '.join(cmd))
subprocess.call(cmd)

print(f'elapsed {time() - start:.2f}s')
37 changes: 37 additions & 0 deletions snippets/py/pack_filetype_from_a_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Pack (Copy or move) all filetype contained in subdirectory

from pathlib import Path
import time

source_path = Path('/folder/containing/videos')
assert source_path.exists(), f'Not found: {source_path}'

print(f'-- Copy Prores from: {source_path}')
date = time.strftime('%Y_%m_%d')

print('Using date: ', date)


file_list = source_path.rglob('*.mov')

dest_folder = Path(f'/path/to/exports/{date}_video_pack/')
dest_folder.parent.mkdir(exist_ok=True, parents=True)

# Root to create subpath by curring root from
root = source_path

for f in file_list:
## subpath
subpath = f.as_posix().replace(root.as_posix(), '').lstrip('/')
# print('subpath: ', subpath)
dest_file = dest_folder / str(subpath)
print('\nsrc:', f)
print('dst:', dest_file)
dest_file.parent.mkdir(exist_ok=True, parents=True)

## Move file to destination
f.rename(dest_file)

## Copy to destination
# import shutil
# shutil.copy(f, dest_file) # need str for python <= 3.7
37 changes: 37 additions & 0 deletions snippets/py/sort_mixed_file_sequences_in_own_subfolders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Move mixed image sequences in their own subfolder

import re
from pathlib import Path
from itertools import groupby

## Directory with sequences to sort

paths = [
'/img_seq_folder_1',
'/img_seq_folder_2',
]

## Make sure the sequence naming convention respect the pattern
## (any non-matching file is skipped)
## Created folder is named after regex group 1 (stem before number padding separator)

re_seq = re.compile(r'(.*?)_\d{4}')

for fp in paths:
fp = Path(fp)

file_list = [f for f in Path(fp).iterdir() if f.is_file() and re_seq.match(f.name)]
## /!\ important ! Sort result before groupby (otherwise multiple groups with using same key)
file_list.sort(key=lambda x: x.name)

for key, group in groupby(file_list, key=lambda x: re_seq.search(x.name).group(1)):
## /!\ Remember, iterable exhaust iself while iterating

dest = fp / key
dest.mkdir(exist_ok=True)

for f in group:
print(f)

file_dest = dest / f.name
f.rename(file_dest)

0 comments on commit fd93875

Please sign in to comment.