-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo_recorder.py
65 lines (49 loc) · 2.31 KB
/
video_recorder.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
import argparse
import os
import signal
import subprocess
import time
from datetime import datetime, timedelta
import pytz
import requests
def record_real_time_stream(video_url, output_prefix, finish_time):
if datetime.now() >= finish_time:
return True
output_path = get_output_path(output_prefix)
script = f"youtube-dl -o {output_path} {video_url}"
process = subprocess.Popen(script, shell=True, preexec_fn=os.setsid)
# process = subprocess.Popen(script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
while datetime.now() < finish_time:
time.sleep(5)
res = process.poll()
if res is not None: # terminated
# out, err = process.communicate()
raise ValueError(f"Recording process terminated")
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
subprocess.run(f"mv {output_path}.part {output_path}", stdout=subprocess.PIPE, shell=True)
def get_output_path(output_prefix):
now = datetime.now(tz=pytz.timezone('Asia/Seoul'))
time_str = now.strftime("%Y-%m-%dT%H:%M:%S")
return f"{output_prefix}-{time_str}.mp4"
def send_slack_message(text, url="https://hooks.slack.com/services/00000/000000"):
payload = {
"text": text
}
requests.post(url, json=payload)
def retry_record(video_url, output_prefix, finish_time, slack_web_hook_url, count=15):
try:
record_real_time_stream(video_url, output_prefix, finish_time)
except Exception as e:
send_slack_message(f"video-recorder ERROR: {e}", slack_web_hook_url)
time.sleep(60)
retry_record(video_url, output_prefix, finish_time, slack_web_hook_url, count - 1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output-prefix", dest="output_prefix", action="store")
parser.add_argument("-d", "--duration", dest="duration", action="store")
parser.add_argument("-v", "--video-url", dest="video_url", action="store")
parser.add_argument("-s", "--slack-web-hook-url", dest="slack_web_hook_url", action="store")
args = parser.parse_args()
finish_time = datetime.now() + timedelta(0, int(args.duration))
print("duration:", args.duration, "finish_time:", finish_time)
retry_record(args.video_url, args.output_prefix, finish_time, args.slack_web_hook_url)