Soldato
In an effort to try and learn Python, and make something useful for me, I'm trying to create a project that semi automates checking for adverts in TV programs I record on Tvheadend (yes I know Tvh has a comskip plugin, but where's the fun in that! ).
I've got the basic program working so it checks the recording folder, looks to see if the recording has already been processed (gets added to a text file when it has been), then runs comskip if it's a new program.
I'm trying to create two functions, that I'm having problems with:
Checking to see if the recordings, that have already been processed and added to the text file, still exist on disk. If not, I want to remove the line from the text file
Checking to see if a recording is currently active so I can ignore it until it's finished
The relevant code bits I've got so far (which don't work):
Any ideas? I've spent the last day searching online and trying different ways, but it's clear I'm doing something wrong as neither of these functions work properly.
I've got the basic program working so it checks the recording folder, looks to see if the recording has already been processed (gets added to a text file when it has been), then runs comskip if it's a new program.
I'm trying to create two functions, that I'm having problems with:
Checking to see if the recordings, that have already been processed and added to the text file, still exist on disk. If not, I want to remove the line from the text file
Checking to see if a recording is currently active so I can ignore it until it's finished
The relevant code bits I've got so far (which don't work):
Python:
import os
import fnmatch
import subprocess
from time import sleep
RECORDINGS = "PATH/TO/RECORDINGS"
VIDEOS = "OUTPUT FOLDER"
COMSKIP = "comskip"
HD_PROGRAMS = " HD "
ARG1 = "--ts"
ARG2 = "--quiet"
ARG3 = "--vdpau"
ARG4 = "--ini=comskip.ini"
ARG5 = "--output=OUTPUT FOLDER"
PROCESSED_FILES = "processed.txt"
def check_processed():
"""Checks to see if we have already processed the recording and added it to processed.txt"""
try:
with open(PROCESSED_FILES, "r") as processed_files:
file_check = processed_files.readlines()
except FileNotFoundError:
with open(PROCESSED_FILES, "w+") as processed_files:
file_check = processed_files.readlines()
else:
for entry in file_check:
if str(file) in entry:
return True # The string is found
return False # The string does not exist in the file
def processed_exist_check():
file = [f for f in os.listdir(RECORDINGS) if fnmatch.fnmatch(f, '*.ts')]
for f in file:
with open(PROCESSED_FILES, "r") as pf:
lines = pf.readlines()
for line in lines:
if line not in f:
print(f"Can't find {line}")
else:
print(f"Found {line}")
# This function keeps failing saying it can't find the files.
def delete_extra_files():
"""Deletes all the extra files comskip creates when scanning for adverts"""
filename = os.path.splitext(file)[0]
os.remove(f"{VIDEOS}{filename}.txt")
os.remove(f"{VIDEOS}{filename}.edl")
os.remove(f"{VIDEOS}{filename}.log")
def file_size_check():
for video in os.listdir(RECORDINGS):
if fnmatch.fnmatch(video, '*.ts'):
size_of_file = [
(video, os.stat(os.path.join(RECORDINGS, video)).st_size)
]
# This just converts the file into MB, and is unnecessary for this project, but nice to know
# print(size_of_file)
# for f, s in size_of_file:
# print("{} : {}MB".format(f, round(s / (1024 * 1024), 3)))
return size_of_file[0][1] # This doesn't work properly, with or without the [0][1]
else:
pass
def is_recording():
# Doesn't work
print("Checking file size")
first_check = file_size_check()
print(first_check)
sleep(5)
print("Rechecking file size to ensure it's not still recording")
second_check = file_size_check()
print(second_check)
if second_check > first_check:
return True
else:
return False
# This function only ever seems to return the file size for the same file over and over so never skips files that are actively recording
# Get the video filenames and see if we've already processed them
processed_exist_check()
for file in os.listdir(RECORDINGS):
if fnmatch.fnmatch(file, '*.ts'):
if check_processed():
pass
elif is_recording():
pass
else:
print(f"Processing {file}")
result = subprocess.run([COMSKIP, ARG1, ARG2, ARG3, ARG4, ARG5, f"{RECORDINGS}{file}"])
with open(PROCESSED_FILES, "a") as completed:
completed.write(f"{file}\n")
delete_extra_files()
Any ideas? I've spent the last day searching online and trying different ways, but it's clear I'm doing something wrong as neither of these functions work properly.
Last edited: