worker.py 1.1 KB
Newer Older
Andreas Valder's avatar
Andreas Valder committed
1
#!/usr/bin/env python3
Andreas Valder's avatar
Andreas Valder committed
2
3
4
import time
import traceback
import configparser
Andreas Valder's avatar
Andreas Valder committed
5
6
import json
import shlex
Andreas Valder's avatar
Andreas Valder committed
7
import sys
Andreas Valder's avatar
Andreas Valder committed
8
9
import subprocess
import os
Andreas Valder's avatar
Andreas Valder committed
10
from workerapi import WorkerApi
Andreas Valder's avatar
Andreas Valder committed
11

Andreas Valder's avatar
Andreas Valder committed
12
13
14
config = configparser.ConfigParser()
config.read('config.ini')

Andreas Valder's avatar
Andreas Valder committed
15
api = WorkerApi(config['API']['BASE'],config['API']['KEY'])
Andreas Valder's avatar
Andreas Valder committed
16

Andreas Valder's avatar
Andreas Valder committed
17
18
19
20
21
id = int(sys.argv[1])
jobtype = sys.argv[2]
priority = sys.argv[3]
data = json.loads(sys.argv[4])
print(sys.argv)
Andreas Valder's avatar
Andreas Valder committed
22

Andreas Valder's avatar
Andreas Valder committed
23
if (jobtype == 'thumbnail'):
Andreas Valder's avatar
Andreas Valder committed
24
25
	inputfile  = config['MAIN']['VIDEOS_RELEASED']+'/'+data['path']
	outputfile = config['MAIN']['VIDEOS_RELEASED']+'/'+config['thumbnail']['folder']+'/'+'l_'+str(data['lectureid'])+'.jpg'
Andreas Valder's avatar
Andreas Valder committed
26
	api.job_ping(id=id)
Andreas Valder's avatar
Andreas Valder committed
27
28
29
30
	try:
		duration = float(subprocess.check_output(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", inputfile]))
	except:
		duration = 0
Andreas Valder's avatar
Andreas Valder committed
31
	api.job_ping(id=id)
Andreas Valder's avatar
Andreas Valder committed
32
	if subprocess.call(["ffmpeg", "-loglevel", "error", "-y", "-ss", str(duration*0.4), "-i", inputfile, "-vf", "scale=640:-1", "-frames:v", "1", outputfile]) == 0:
Andreas Valder's avatar
Andreas Valder committed
33
34
35
36
		api.job_ping(id=id,state='finished')
	else:
		api.job_ping(id=id,state='failed')
		print('failed')