Fixes failed to delete images because the timing of gc is uncertain (#1710)
* Prevents resource leak in video_utils when getting width and height Added the with statement when opening the image to ensure that the file handle is properly closed after its contents are read. Otherwise, shutil.rmtree(img_dir) will fail when called after the encode_video_frames function completes. Signed-off-by: Lycoris <32864669+lycoris1129@users.noreply.github.com> --------- Signed-off-by: Lycoris <32864669+lycoris1129@users.noreply.github.com>
This commit is contained in:
@@ -342,8 +342,8 @@ def encode_video_frames(
|
|||||||
# Define video output frame size (assuming all input frames are the same size)
|
# Define video output frame size (assuming all input frames are the same size)
|
||||||
if len(input_list) == 0:
|
if len(input_list) == 0:
|
||||||
raise FileNotFoundError(f"No images found in {imgs_dir}.")
|
raise FileNotFoundError(f"No images found in {imgs_dir}.")
|
||||||
dummy_image = Image.open(input_list[0])
|
with Image.open(input_list[0]) as dummy_image:
|
||||||
width, height = dummy_image.size
|
width, height = dummy_image.size
|
||||||
|
|
||||||
# Define video codec options
|
# Define video codec options
|
||||||
video_options = {}
|
video_options = {}
|
||||||
@@ -373,11 +373,12 @@ def encode_video_frames(
|
|||||||
|
|
||||||
# Loop through input frames and encode them
|
# Loop through input frames and encode them
|
||||||
for input_data in input_list:
|
for input_data in input_list:
|
||||||
input_image = Image.open(input_data).convert("RGB")
|
with Image.open(input_data) as input_image:
|
||||||
input_frame = av.VideoFrame.from_image(input_image)
|
input_image = input_image.convert("RGB")
|
||||||
packet = output_stream.encode(input_frame)
|
input_frame = av.VideoFrame.from_image(input_image)
|
||||||
if packet:
|
packet = output_stream.encode(input_frame)
|
||||||
output.mux(packet)
|
if packet:
|
||||||
|
output.mux(packet)
|
||||||
|
|
||||||
# Flush the encoder
|
# Flush the encoder
|
||||||
packet = output_stream.encode()
|
packet = output_stream.encode()
|
||||||
|
|||||||
Reference in New Issue
Block a user