Создание видео из изображений (python)Python

Программы на Python
Ответить
Anonymous
 Создание видео из изображений (python)

Сообщение Anonymous »

Мне удалось написать следующий код для объединения нескольких изображений в один файл mp4. Однако файл, созданный таким образом, «поврежден». Есть ли у кого-нибудь идеи, что может произойти?
def create_video_from_images(self, video_name='output_video.mp4'):
"""
Creates a video from images in a specified
"""
# Get sorted list of image files from the folder
image_files = sorted(
[os.path.join(self.images_dir, f) for f in os.listdir(self.images_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
)

# Read the first image to determine video dimensions
first_image = cv2.imread(image_files[0])
height, width, _ = first_image.shape

# Calculate frames per image
total_frames = int(30 * self.length) # Assuming 30 frames per second
frames_per_image = int(total_frames // len(image_files))

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4 files
output_file = os.path.join(self.video_dir, video_name)
video_writer = cv2.VideoWriter(output_file, fourcc, 30, (width, height))

for image_file in image_files:
img = cv2.imread(image_file)
if img.shape[:2] != (height, width):
img = cv2.resize(img, (width, height)) # Resize if dimensions differ
for _ in range(frames_per_image):
video_writer.write(img)

video_writer.release()


Подробнее здесь: https://stackoverflow.com/questions/792 ... ges-python
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»