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
#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


Messages In This Thread
RE: Help in designing a timestamp finder for chapter-less TV shows - by Daring_T - Oct-26-2020, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ModuleNotFound but pip shows module installed biscotty666 2 1,558 Jul-14-2022, 05:17 PM
Last Post: Axel_Erfurt
  error in timestamp Led_Zeppelin 3 3,227 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,011 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Os command output in variable shows wrong value paulo79 2 1,514 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  problem about maze path finder Simurg 2 1,953 Aug-16-2020, 01:10 PM
Last Post: Simurg
  Designing a "game" loop api pitosalas 2 2,482 Jun-07-2020, 03:20 PM
Last Post: pitosalas
  colorbar for scatter shows no negatives values... gil 0 1,539 Apr-15-2020, 12:45 AM
Last Post: gil
  ISS Location Finder problem birdwatcher 7 3,634 Jan-20-2020, 07:41 PM
Last Post: birdwatcher
  after using openpyxl to add colors to script, black shows up white online in excel Soundtechscott 1 3,697 Jun-08-2019, 10:33 PM
Last Post: Soundtechscott
  Stack trace shows different exception type than print micseydel 5 4,423 Apr-01-2019, 10:24 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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