Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 7aa0405

Browse files
committed
add batch video frame extract option
1 parent 6cca016 commit 7aa0405

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

main.py

+11
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ def process_videoed_extract_video(arguments):
201201
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.")
202202
p.set_defaults(func=process_videoed_extract_video)
203203

204+
def process_videoed_batch_extract_video(arguments):
205+
osex.set_process_lowest_prio()
206+
from mainscripts import VideoEd
207+
VideoEd.batch_extract_video (arguments.input_dir, arguments.output_dir, arguments.output_ext, arguments.fps)
208+
p = videoed_parser.add_parser( "batch-extract-video", help="Extract images from folder of video files.")
209+
p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir", help="Input directory of files to be processed.")
210+
p.add_argument('--output-dir', required=True, action=fixPathAction, dest="output_dir", help="Output directory. This is where the extracted images will be stored.")
211+
p.add_argument('--output-ext', dest="output_ext", default=None, help="Image format (extension) of output files.")
212+
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.")
213+
p.set_defaults(func=process_videoed_batch_extract_video)
214+
204215
def process_videoed_cut_video(arguments):
205216
osex.set_process_lowest_prio()
206217
from mainscripts import VideoEd

mainscripts/VideoEd.py

+48
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,54 @@ def extract_video(input_file, output_dir, output_ext=None, fps=None):
4848
except:
4949
io.log_err ("ffmpeg fail, job commandline:" + str(job.compile()) )
5050

51+
def batch_extract_video(input_dir, output_dir, output_ext=None, fps=None):
52+
import os
53+
input_path = Path(input_dir)
54+
output_path = Path(output_dir)
55+
56+
if not output_path.exists():
57+
output_path.mkdir(exist_ok=True)
58+
59+
if not input_path.exists():
60+
io.log_err("input_dir not found.")
61+
return
62+
63+
if not input_path.is_dir():
64+
io.log_info("input_dir is not a directory.")
65+
return
66+
67+
file_paths = pathex.get_video_paths(input_path, return_Path_class=True)
68+
io.log_info('\n'.join(f'file found ...... {file.name}' for file in file_paths))
69+
io.log_info(f'{len(file_paths)} files')
70+
71+
file_check = io.input_str ("Are these the intended files?", "y", ["y", "n"])
72+
if not file_check == 'y': return
73+
74+
if fps is None:
75+
fps = io.input_int ("Enter FPS", 0, help_message="How many frames of every second of the video will be extracted. 0 - full fps")
76+
77+
if output_ext is None:
78+
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.")
79+
80+
for filename in pathex.get_image_paths (output_path, [f'.{output_ext}']):
81+
Path(filename).unlink()
82+
83+
kwargs = {
84+
'pix_fmt': 'rgb24',
85+
**({'r':f'{fps}'} if fps else {}),
86+
**({'q:v':'2'} if output_ext == 'jpg' else {}),
87+
}
88+
89+
for idx, file_path in enumerate(file_paths):
90+
job = ( ffmpeg
91+
.input(str(file_path))
92+
.output(str(output_path / (f'{idx:03}_%5d.{output_ext}')), **kwargs)
93+
)
94+
try:
95+
job = job.run()
96+
except:
97+
io.log_err ("ffmpeg fail, job commandline:" + str(job.compile()))
98+
5199
def cut_video ( input_file, from_time=None, to_time=None, audio_track_id=None, bitrate=None):
52100
input_file_path = Path(input_file)
53101
if input_file_path is None:

0 commit comments

Comments
 (0)