Python Forum
Help in designing a timestamp finder for chapter-less TV shows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help in designing a timestamp finder for chapter-less TV shows
#1
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
Reply
#2
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
bowlofred likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Working with Timestamp in Mulitindex krischanb 0 213 Jan-22-2021, 12:52 PM
Last Post: krischanb
  problem about maze path finder Simurg 2 485 Aug-16-2020, 01:10 PM
Last Post: Simurg
  Designing a "game" loop api pitosalas 2 600 Jun-07-2020, 03:20 PM
Last Post: pitosalas
  colorbar for scatter shows no negatives values... gil 0 451 Apr-15-2020, 12:45 AM
Last Post: gil
  ISS Location Finder problem birdwatcher 7 1,052 Jan-20-2020, 07:41 PM
Last Post: birdwatcher
  Replace last 4 bytes of a timestamp mPlummers 18 2,306 Sep-17-2019, 12:47 AM
Last Post: Larz60+
  How to convert different timestamp formats? python_newbie09 12 2,055 Aug-05-2019, 06:37 PM
Last Post: python_newbie09
  after using openpyxl to add colors to script, black shows up white online in excel Soundtechscott 1 1,352 Jun-08-2019, 10:33 PM
Last Post: Soundtechscott
  Stack trace shows different exception type than print micseydel 5 1,869 Apr-01-2019, 10:24 PM
Last Post: micseydel
  Timestamp is undefined ErnestTBass 7 2,542 Feb-16-2019, 08:27 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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