-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoder.py
executable file
·72 lines (58 loc) · 1.87 KB
/
encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 1. Fetch job metadata from master
# 2. Download video from S3
# 3. Encode video into HLS
# 4. Encode mp4 video to different flavors and check into db
# 4. Generate main m3u8 files
# 5. Upload video to S3
# 6. Report job complete
import logging, os, sys, ConfigParser
from api import ApiManager
from datetime import datetime, timedelta
def main():
init('/home/ec2-user/settings.ini')
# First check if the script is already running
pid = str(os.getpid())
pid_file = "/tmp/encoder.pid"
if os.path.isfile(pid_file):
hours_ago = datetime.now() - timedelta(hours=12)
file_time = datetime.fromtimestamp(os.path.getctime(pid_file))
if file_time < hours_ago:
# pid too old, delete
logging.info("%s is stale removed" % pid_file)
os.remove(pid_file)
else:
# encoder is still running
logging.warning("%s already exists, exiting" % pid_file)
sys.exit()
file(pid_file, 'w').write(pid)
# Get job from api
api = ApiManager()
job = api.get_job()
#job = api.getLocalJob()
if job.id != 0:
logging.info("### JOB START ###")
try:
job.download_file()
job.generate_hls(api)
job.generate_mp4(api)
job.transfer_S3()
job.cleanup()
except Exception as e:
job.status = 'Job Error: ' + e.__str__()
api.checkin_job(job)
logging.info("### JOB END ###")
os.unlink(pid_file)
def init(settings_file):
config = ConfigParser.ConfigParser()
config.read(settings_file)
# Setup Logging
logging.basicConfig(
filename=config.get('Encoder','log_file'),
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO
)
if __name__ == '__main__':
main()