Python Forum
Better way to append frames from a video to multiple lists?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better way to append frames from a video to multiple lists?
Hi, I am trying to split a video based on the index and value stored in a text file. The index represents the frame number and value represents frame class. Then I create multiples lists based on the count of unique values in the text file and split the videos and append the frames to a list based on the frame number. But this operation on a larger video crashes my python because of memory issues. Is there a better way to do this? Here is an example text

In the code, I am referring to line 28. Thanks for your time.

import numpy as np
import cv2
import os
from collections import defaultdict

# Create a generator to let you loop over the video in a for loop
def get_frames(vid):
    while True:
        ret, frame =
        if not ret:
        yield frame

def split_videos(txt_file, video_file):

    with open(txt_file,'r') as f:
        frame_numbers = list(map(int,
        size = len(set(frame_numbers))

    offset = min(frame_numbers)
    frame_groups = [[] for _ in range(7)]

    video = cv2.VideoCapture(video_file)

    for index, frame in zip(frame_numbers, get_frames(video)):
        frame_groups[index - offset].append(frame)


    frame_groups_final = [x for x in frame_groups if x]
    codec = cv2.VideoWriter_fourcc(*'XVID')

    for index, group in enumerate(frame_groups_final):
    # Arguments are filename, codec, framerate, resolution
        print((len(group[0]), len(group[0][0])))
        writer = cv2.VideoWriter(f'video_{index + offset}.avi', codec, 30.0, (len(group[0][0]), len(group[0])))
        for frame in group:

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Dict from multiple Lists with duplicate Keys rhat398 10 551 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 580 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 658 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Append 2d empty lists NMMST 2 537 Oct-19-2020, 09:25 PM
Last Post: NMMST
  Reading Multiple Lists Using SUM function dgrunwal 6 1,203 Jun-03-2020, 08:23 PM
Last Post: dgrunwal
  Inserting values from multiple lists sqlite azulu 1 804 May-24-2020, 08:40 AM
Last Post: ibreeden
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 775 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  how do i create average of 1000 frames per second from video with yolo Rupen_gurung 0 558 Jul-20-2019, 01:55 AM
Last Post: Rupen_gurung
  Evaluating multiple lists in a tuple cdogo 2 1,022 Apr-14-2019, 09:15 AM
Last Post: Yoriz
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 1,153 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020