-
Notifications
You must be signed in to change notification settings - Fork 88
/
clip.py
46 lines (35 loc) · 1.23 KB
/
clip.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
import requests,re
import os,argparse,subprocess
from datetime import timedelta
def clipTime(s,e):
time = " -ss "+s+" -to "+e+" "
return time
parser = argparse.ArgumentParser()
parser.add_argument("--url", help="video url",type=str)
parser.add_argument("--offset", help="Clip time offset",type=float)
args = parser.parse_args()
url = args.url
if args.offset is not None:
offset = args.offset
else:
offset = 5
offsetMs=int(offset*1000)
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
pattern=r'("startTimeMs.*"endTimeMs":"[0-9]+")'
timeConfig=re.search(pattern,response.text)
time=re.findall(r"\d+\.?\d*",timeConfig.group(0))
cmd="yt-dlp -g --youtube-skip-dash-manifest -S \"ext\" --no-warnings "+args.url
r = os.popen(cmd)
urlOutput = r.read()
r.close()
urls=urlOutput.split('\n',1)
start=str(timedelta(milliseconds=int(time[0])-offsetMs))[0:7]
end=str(timedelta(milliseconds=int(time[1])+offsetMs))[0:7]
trimCmd="ffmpeg"+clipTime(start,end)+"-i \""+urls[0].replace('\n', '').replace('\r', '')+"\""
if(len(urls[1])>0):
trimCmd+=clipTime(start,end)+"-i "+"\""+urls[1].replace('\n', '').replace('\r', '')+"\""
trimCmd+=" output.mp4"
print(trimCmd)
subprocess.run(trimCmd,shell=True)