Skip to content

Instantly share code, notes, and snippets.

@syehoonkim
Created November 30, 2023 04:17
Show Gist options
  • Select an option

  • Save syehoonkim/4d4e93ea90315d638ef8c08d0c6d005a to your computer and use it in GitHub Desktop.

Select an option

Save syehoonkim/4d4e93ea90315d638ef8c08d0c6d005a to your computer and use it in GitHub Desktop.
File Watchdog
import glob, os, time, subprocess, threading, datetime
def updatelog(text):
f = '/var/log/sbsonair.log'
text = str(text)
tm_stamp = datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S.%f ")
print(tm_stamp + text)
with open(f, "a") as handle:
handle.write(tm_stamp + text + '\n')
class filemanager:
def get_ts(self, path):
print(f'path is {path}')
key = os.path.join(path, '**/*.ts')
print(f'search pattern is {key}')
flist = glob.glob(key, recursive=True)
flist.sort()
return flist
def get_age(self, infile):
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(infile)
return time.time() - ctime
def get_mp4_name(self, infile):
#head, tail = os.path.split(infile)
(name, extension) = os.path.splitext(infile)
return name + '.mp4'
fm = filemanager()
timer = ''
def gather_n_transcode():
global timer, fm
updatelog("Start gather loop....")
try:
for f in fm.get_ts('/mnt/recordings'):
print(f, end=" ")
age = fm.get_age(f)
mp4file = fm.get_mp4_name(f)
is_mp4file = os.path.exists(fm.get_mp4_name(f))
str_run = "ffmpeg -i " + f + " -vf scale=960:540 -c:v h264 -preset:v veryfast -g 15 -b:v 2000k -c:a aac -b:a 128k -threads 0 -movflags faststart -y " + mp4file
print(age, end=" ")
print(mp4file, end=" ")
print(is_mp4file)
print(str_run.split(" "))
if (age > 6000):
updatelog(f'start mp4 encoding.... {f}')
result = subprocess.run(str_run.split(" "))
updatelog(result)
updatelog(f'removing file.. {f}')
os.remove(f)
except Exception as e:
updatelog(f'Error in gather loop {e}')
timer = threading.Timer(60, gather_n_transcode)
timer.start()
updatelog("Start application......")
t_start = time.time()
gather_n_transcode()
try:
while True:
print(f'sendust encoder is running {time.time() - t_start}', end='\r')
time.sleep(0.1)
except KeyboardInterrupt:
timer.cancel()
updatelog("Finish application......")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment