-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
279 lines (219 loc) · 7.64 KB
/
Copy pathtools.py
File metadata and controls
279 lines (219 loc) · 7.64 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from langchain_community.tools import YouTubeSearchTool
from langchain_core.tools import tool
import subprocess
import requests
from subtitle_fix import shift_subtitles_to_zero_start , srt_to_ass
yt_tool = YouTubeSearchTool()
import glob
from typing import Any
from pydantic import BaseModel, Field
class TrimMediaInput(BaseModel):
input_file: str = Field(...)
output_file: str = Field(...)
start_time: str = Field(...)
end_time: str = Field(...)
import json
import os
@tool
def report_error(error: str) -> None:
"""
Report errors during processing.
Args:
error (str): The error message to report.
This function is intended to help with debugging by logging or
notifying about runtime issues.
"""
print("Reporting error ::", error)
@tool
def get_youtube_object(video_id: str) -> dict:
"""
Fetches the description
Args:
video_id(str) : The youtube Id of the object
Returns:
return the description of the video
"""
video_url = f"https://www.youtube.com/watch?v={video_id}"
cookies_path = "./cookie.txt"
cmd = [
"yt-dlp",
"--cookies", cookies_path,
"--skip-download",
"--quiet",
"--no-warnings",
"--print-json",
video_url
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
info = json.loads(result.stdout)
subtitles = info.get("automatic_captions", {})
lang_code = next((code for code in subtitles if code.endswith("-orig")), None)
return info.get('description') , lang_code
except subprocess.CalledProcessError as e:
send_video.invoke("COOKIE EXPIRED")
print(f"[ERROR] yt-dlp failed: {e.stderr}")
except json.JSONDecodeError as e:
print(f"[ERROR] Failed to parse JSON output: {e}")
return None
from pydantic import BaseModel, Field
def trim_media(input: TrimMediaInput):
"""
Trim a media file (audio/video) using FFmpeg.
Args:
input_file (str): Full path to the original media file.
output_file (str): Desired path for the trimmed output file.
start_time (str): Trim start time ('HH:MM:SS' or seconds).
end_time (str): Trim end time ('HH:MM:SS' or seconds).
The function uses FFmpeg to extract a specific portion of the file without re-encoding.
"""
def to_seconds(t):
if isinstance(t, (int, float)):
return float(t)
parts = [float(p) for p in t.split(':')]
if len(parts) == 3:
return parts[0]*3600 + parts[1]*60 + parts[2]
elif len(parts) == 2:
return parts[0]*60 + parts[1]
else:
return parts[0]
start_sec = to_seconds(input["start_time"])
end_sec = to_seconds(input["end_time"])
duration = end_sec - start_sec
command = [
'ffmpeg',
'-y',
'-ss', str(start_sec),
'-i', input["input_file"],
'-t', str(duration),
'-c', 'copy',
input["output_file"]
]
try:
subprocess.run(command, check=True)
print(f"Trimmed media saved to {input['output_file']}")
except subprocess.CalledProcessError as e:
print("Error trimming media:", e)
def youtube_tool(video_id:str, lang:str, output_path: str = "./data/current_podcast.mp4"):
"""
Downloads a YouTube video using yt-dlp subprocess and a cookie file.
Args:
info (Any): The video info object or video ID/URL.
lang (str) : Subtitle code
output_path (str): Path to save the downloaded file.
"""
video_url = "https://youtube.com/watch?v="+video_id
if not video_url:
raise ValueError("Invalid video info or missing URL.")
cmd = [
"yt-dlp",
"--cookies", "./cookie.txt",
"-f", "best",
"--write-auto-sub",
"--sub-format" , "srt",
"--sub-langs" , lang,
'-o' ,"./data/current_podcast.%(ext)s",
video_url
]
try:
subprocess.run(cmd, check=True)
print(f"[SUCCESS] Video downloaded to: {output_path}")
except subprocess.CalledProcessError as e:
send_video.invoke("COOKIE EXPIRED")
print(f"[ERROR] Download failed:\n{e}")
filepath = glob.glob('./data/current_podcast*.srt')[0]
return filepath
def convert_and_add_captions(input_video: str, srt_file: str, output_video: str):
"""
Convert a video to 9:16 aspect ratio, scale to HD, and burn subtitles.
Args:
input_video (str): Path to the input video file.
srt_file (str): Path to the subtitle (.srt) file to burn in.
output_video (str): Output filename for the final video.
This function:
- Crops to 9:16 portrait ratio.
- Scales to 720x1280.
- Hardcodes (burns) subtitles into the video using FFmpeg.
"""
d = open(srt_file , "r" , encoding='utf-8')
data_subtitle = d.read()
shifted_subtitles = shift_subtitles_to_zero_start(data_subtitle)
open(srt_file , 'w+' , encoding='utf-8').write(shifted_subtitles)
srt_to_ass(srt_file , srt_file+"-.ass")
filter_chain = (
f"subtitles={srt_file+'-.ass'},"
"split[original][copy];"
"[copy]scale=-1:ih*(16/9)*(16/9),"
"crop=w=ih*9/16,"
"gblur=sigma=20[blurred];"
"[blurred][original]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2"
)
command = [
"ffmpeg",
"-i", input_video,
"-vf", filter_chain,
"-y",
output_video
]
print("Running FFmpeg command:")
print(" ".join(command))
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if process.returncode == 0:
print(f"Successfully created {output_video}")
else:
print("Error running FFmpeg:")
print(process.stderr)
def print_green(text):
print(f"\033[92m{text}\033[0m")
def print_yellow(text):
print(f"\033[93m{text}\033[0m")
def upload_video(file_path , metadata ):
"""
Uploads the video yo YouTube
Args:
file_path: The path of the file to upload
metadata : dict of the metadata to upload
{
title:"Title of the video",
description:"Description of the video",
privacyStatus:"public",
keywords=["surfing" , "second"],
category = "22"
}
"""
command = [
"python",
"yt_upload.py",
"--file" , file_path ,
"--title", metadata['title'],
"--privacyStatus" , "public",
"--keywords" , ",".join(metadata["keywords"]),
"--category", "27",
"--description" , metadata["description"]
]
print(" ".join(command))
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if process.returncode == 0:
print_green(f"Successfully uploaded {file_path}")
else:
print("Error running FFmpeg:")
print(process.stderr)
send_video.invoke("YT UPLOAD EXPIRED")
@tool
def send_video(text:str):
"""
This tool sends message to the developers's telegram
Args:
text - The message to send to developer
"""
token = 'yout-bot-token-here'
chatId = 'your-chat-id-here'
send_msg_url = f"https://api.telegram.org/bot{token}/sendMessage"
data = {
"chat_id": chatId,
"text" : text
}
requests.post(send_msg_url, data=data)
if __name__ == '__main__':
obj = get_youtube_object.invoke("r8pDXO6zRUg")
print(youtube_tool(obj))