This commit is contained in:
Simon 2024-11-15 14:25:44 +08:00 committed by GitHub
commit ac4aa27535
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 94 additions and 28 deletions

View file

@ -2,6 +2,7 @@ from pathlib import Path
from os import scandir
image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
video_extensions = [".mp4", ".mov", ".mpg", ".wmv", ".mkv", ".avi", ".avchd", ".flv", ".f4v", ".swf", "webm"]
def write_bytes_safe(p, bytes_data):
"""
@ -21,7 +22,7 @@ def scantree(path):
else:
yield entry
def get_image_paths(dir_path, image_extensions=image_extensions, subdirs=False, return_Path_class=False):
def get_extension_paths(dir_path, extensions, subdirs=False, return_Path_class=False):
dir_path = Path (dir_path)
result = []
@ -32,10 +33,16 @@ def get_image_paths(dir_path, image_extensions=image_extensions, subdirs=False,
else:
gen = scandir(str(dir_path))
for x in list(gen):
if any([x.name.lower().endswith(ext) for ext in image_extensions]):
result.append( x.path if not return_Path_class else Path(x.path) )
return sorted(result)
for x in gen:
if any(x.name.lower().endswith(ext) for ext in extensions): # listcomp is not needed, any() can handle gencomp
result.append( Path(x) if return_Path_class else x.path ) # avoid unnecessary negative condition
return result # scandir should already be sorted
def get_image_paths(dir_path, image_extensions=image_extensions, subdirs=False, return_Path_class=False):
return get_extension_paths(dir_path, image_extensions, subdirs, return_Path_class)
def get_video_paths(dir_path, video_extensions=video_extensions, subdirs=False, return_Path_class=False):
return get_extension_paths(dir_path, video_extensions, subdirs, return_Path_class)
def get_image_unique_filestem_paths(dir_path, verbose_print_func=None):
result = get_image_paths(dir_path)

11
main.py
View file

@ -201,6 +201,17 @@ if __name__ == "__main__":
p.add_argument('--fps', type=int, dest="fps", default=None, help="How many frames of every second of the video will be extracted. 0 - full fps.")
p.set_defaults(func=process_videoed_extract_video)
def process_videoed_batch_extract_video(arguments):
osex.set_process_lowest_prio()
from mainscripts import VideoEd
VideoEd.batch_extract_video (arguments.input_dir, arguments.output_dir, arguments.output_ext, arguments.fps)
p = videoed_parser.add_parser( "batch-extract-video", help="Extract images from folder of video files.")
p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir", help="Input directory of files to be processed.")
p.add_argument('--output-dir', required=True, action=fixPathAction, dest="output_dir", help="Output directory. This is where the extracted images will be stored.")
p.add_argument('--output-ext', dest="output_ext", default=None, help="Image format (extension) of output files.")
p.add_argument('--fps', type=int, dest="fps", default=None, help="How many frames of every second of the video will be extracted. 0 - full fps.")
p.set_defaults(func=process_videoed_batch_extract_video)
def process_videoed_cut_video(arguments):
osex.set_process_lowest_prio()
from mainscripts import VideoEd

View file

@ -27,7 +27,7 @@ def extract_video(input_file, output_dir, output_ext=None, fps=None):
fps = io.input_int ("Enter FPS", 0, help_message="How many frames of every second of the video will be extracted. 0 - full fps")
if output_ext is None:
output_ext = io.input_str ("Output image format", "png", ["png","jpg"], help_message="png is lossless, but extraction is x10 slower for HDD, requires x10 more disk space than jpg.")
output_ext = io.input_str ("Output image format", "jpg", ["png","jpg"], help_message="png is lossless, but extraction is x10 slower for HDD, requires x10 more disk space than jpg.")
for filename in pathex.get_image_paths (output_path, ['.'+output_ext]):
Path(filename).unlink()
@ -48,6 +48,54 @@ def extract_video(input_file, output_dir, output_ext=None, fps=None):
except:
io.log_err ("ffmpeg fail, job commandline:" + str(job.compile()) )
def batch_extract_video(input_dir, output_dir, output_ext=None, fps=None):
import os
input_path = Path(input_dir)
output_path = Path(output_dir)
if not output_path.exists():
output_path.mkdir(exist_ok=True)
if not input_path.exists():
io.log_err("input_dir not found.")
return
if not input_path.is_dir():
io.log_info("input_dir is not a directory.")
return
file_paths = pathex.get_video_paths(input_path, return_Path_class=True)
io.log_info('\n'.join(f'file found ...... {file.name}' for file in file_paths))
io.log_info(f'{len(file_paths)} files')
file_check = io.input_str ("Are these the intended files?", "y", ["y", "n"])
if not file_check == 'y': return
if fps is None:
fps = io.input_int ("Enter FPS", 0, help_message="How many frames of every second of the video will be extracted. 0 - full fps")
if output_ext is None:
output_ext = io.input_str ("Output image format", "jpg", ["png","jpg"], help_message="png is lossless, but extraction is x10 slower for HDD, requires x10 more disk space than jpg.")
for filename in pathex.get_image_paths (output_path, [f'.{output_ext}']):
Path(filename).unlink()
kwargs = {
'pix_fmt': 'rgb24',
**({'r':f'{fps}'} if fps else {}),
**({'q:v':'2'} if output_ext == 'jpg' else {}),
}
for idx, file_path in enumerate(file_paths):
job = ( ffmpeg
.input(str(file_path))
.output(str(output_path / (f'{idx:03}_%5d.{output_ext}')), **kwargs)
)
try:
job = job.run()
except:
io.log_err ("ffmpeg fail, job commandline:" + str(job.compile()))
def cut_video ( input_file, from_time=None, to_time=None, audio_track_id=None, bitrate=None):
input_file_path = Path(input_file)
if input_file_path is None: