Python Forum
Help in designing a timestamp finder for chapter-less TV shows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help in designing a timestamp finder for chapter-less TV shows (/thread-26742.html)



Help in designing a timestamp finder for chapter-less TV shows - Daring_T - May-12-2020

Hi! Darin_T here,
I have older TV shows that don`t have any chapter files and its hard manually finding the timestamps after episode and episode and episode...


This is my concept, is this the best way to accomplish my goal?

opens the video file
loops frame by frame
____gets the average brightness of the some or all of the pixels.
____saves the darkest frames as timestamps in a file

I wasn't thinking audio would be a good choose to find timestamps because audio does raise "most" of the time but it varies from episode. Even Though doing it be frames will be slow it will be more accurate.

What libraries would you suggest?

Thanks for Reading,
Darin_T


RE: Help in designing a timestamp finder for chapter-less TV shows - Daring_T - Oct-26-2020

I ended up using cv2:

I finished the first version of my timestamp finder, I works... sort of, its a bit of a hit or miss on some chapter timestamps and sometimes there's a dark scene getting flagged.

import cv2
import numpy as np
import time
import os
from TS import TS


def convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return "%d:%02d:%02d" % (hour, minutes, seconds)


def frames_to_secs(input_frame_num, fps):
    # Python Program to Convert seconds
    # into hours, minutes and seconds
    secs = input_frame_num / fps
    return convert(secs)


def get_file_data(f):
    import re
    f = os.path.basename(f)
    ep_seires, ep_se, ep_title = f.split(" - ")
    match = re.match(r"[S|s](\d\d?)[E|e](\d\d?)", ep_se)
    seasion_num, ep_num = match.groups()
    return ep_seires, seasion_num, ep_num


def remove_duplicates(input_list):
    print('Removing Duplicates from list')
    cleaned_list = list(set(input_list))
    return sorted(cleaned_list)


def get_dark_frames(input_loc):
    """Function to extract frames from input video file
    and save them as timestamps in an output directory.
    Args:
        input_loc: Input video file path.
    Returns:
        list: Found Timestamps
    """

    # Log the time
    time_start = time.time()
    # Start capturing the feed
    cap = cv2.VideoCapture(input_loc)
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Find the number of frames
    video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
    print("Number of frames: ", video_length)
    count = 0

    old_found_TS = ""
    # Start converting the video
    black = num_of_frames = 0
    found_TS_list = []
    while cap.isOpened():

        # Extract the frame
        ret, frame = cap.read()
        num_of_frames = num_of_frames + 1
        # print(num_of_frames)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if np.average(gray) < 7:
            # Write the results back to output location.
            black = black + 1
            found_TS = frames_to_secs(num_of_frames, fps=fps)
            # print("Found_TS:", found_TS)
            found_TS_list.append(found_TS)

        count = count + 1

        # If there are no more frames left
        if count > video_length - 1:
            # Log the time again
            time_end = time.time()
            # Release the feed
            cap.release()
            # print("Found_TS:",TS)
            print("It took %d seconds forconversion." %
                  (time_end - time_start))
            print(f"black frames:{black}")
            print("video_length:", video_length)

            print(get_file_data(input_loc))

            return remove_duplicates(found_TS_list)


input_loc = r"C:\Users\Daren\Videos\Little_House_in_the_Parie\S1\D1 S1\Little_House_in_the_Parie - s01e01 - A Harvest of Friends.mkv"
output_loc = "C:\\Users\\Daren\\Videos\\Testing_Video_files\\"
print(get_dark_frames(input_loc))
Output:
Number of frames: 82677 It took 174 seconds forconversion. black frames:425 video_length: 82677 Removing Duplicates from list ['0:00:00', '0:00:01', '0:02:25', '0:02:26', '0:02:27', '0:02:28', '0:02:29', '0:02:30', '0:02:31', '0:11:39', '0:11:40', '0:22:10', '0:22:11', '0:35:06', '0:35:07', '0:35:08', '0:43:12', '0:43:18', '0:43:20', '0:43:22', '0:43:23', '0:43:26', '0:45:28', '0:45:29', '0:45:57', '0:45:58']
The actual chapter timestamps are:
0:11:39
0:22:10
0:35:06
0:45:28
0:45:57

I just wanted to give an update.
Thanks for Reading,
Daring_T