Repository navigation
Expand file tree
/
Copy pathSunday_Service.py
More file actions
executable file
·390 lines (318 loc) · 12 KB
/
Copy pathSunday_Service.py
File metadata and controls
executable file
·390 lines (318 loc) · 12 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os
import sys
import shutil
import ffmpeg
import subprocess
# import subprocess
from yt_dlp import YoutubeDL
def menu():
print("""
Sunday Service
1. Change video format
2. Extract Audio
3. Bind Audio
4. Change Gain
5. Trim Video
6. Generate Thumbnails
7. Download Video
0. Exit
""")
command = int(input("Enter command: "))
match command:
case 1:
change_video_format()
case 2:
extract_audio()
case 3:
bind_audio()
case 4:
change_gain()
case 5:
trim_video()
case 6:
generate_thumbnails()
case 7:
download_video()
case 712:
video_filename = download_video()
new_video_filename = change_video_format(filename=video_filename)
extract_audio(filename=new_video_filename)
os.remove(video_filename)
case 0:
exit()
def change_video_format(**kwaargs):
"""
Change the format of a video file using ffmpeg.
This function prompts the user for input and output file details,
then uses ffmpeg to convert the video to the specified format.
The function does not modify the video or audio codecs, only the container format.
Returns:
None
"""
if kwaargs.get('filename') is None:
fileLocation = input("Enter input filepath + extension: ")
else:
fileLocation = kwaargs['filename']
fileExtensionInput = fileLocation.split(".")[-1]
fileExtensionOutput = input("Enter container of output: ")
if "." in fileExtensionOutput:
fileExtensionOutput = fileExtensionOutput.replace(".", "")
outputFilename = fileLocation.replace(f".{fileExtensionInput}", "."+fileExtensionOutput)
(
ffmpeg.input(fileLocation)
.output(
f"{outputFilename}",
vcodec="copy",
acodec="copy",
)
.run(overwrite_output=True)
)
return outputFilename
# subprocess.run(f"ffmpeg -i {fileLocation} {fileLocation.replace(f".{fileExtensionInput}", fileExtensionOutput)}", shell=True)
def extract_audio(**kwaargs):
"""
Extract audio from a video file using ffmpeg.
This function prompts the user for input and output file details,
then uses ffmpeg to extract the audio from the video file.
The extracted audio is saved in PCM 32-bit floating-point little-endian format.
Returns:
None
"""
if kwaargs.get('filename') is None:
fileInputLocation = input("Enter audio input filepath + extension: ")
else:
fileInputLocation = kwaargs['filename']
fileOutputName = input("Enter audio output filename + extension: ")
(
ffmpeg.input(fileInputLocation)
.output(
fileInputLocation.replace(
os.path.basename(fileInputLocation), fileOutputName
),
format=fileOutputName.split(".")[-1],
acodec="pcm_f32le",
)
.run(overwrite_output=True)
)
# subprocess.run(f"ffmpeg -i {fileLocation} -vn -c:a pcm_f32le {fileOutputName}", shell=True)
def bind_audio():
"""
Bind audio to a video file using ffmpeg.
This function prompts the user for input video and audio file paths,
as well as the desired output filename. It then uses ffmpeg to merge
the audio from the specified audio file with the video, creating a new
output file with the combined audio and video.
The function uses the 'amerge' filter to combine the audio streams,
and preserves the original video format.
Returns:
None
"""
videoFileLocation = input("Enter input video filepath: ")
audioFileLocation = input("Enter input audio filepath: ")
fileOutputName = input("Enter output filename + extension: ")
outputFormat = videoFileLocation.split(".")[-1]
(
ffmpeg.input(videoFileLocation)
.input(audioFileLocation)
.filter("amerge", inputs=2, outputs=1)
.output(fileOutputName, format=outputFormat)
.run(overwrite_output=True)
)
# subprocess.run(f"ffmpeg -i {videoFileLocation} -i {audioFileLocation} -filter_complex amerge -ac 2 -c:v copy {fileOutputName}", shell=True)
def change_gain():
"""
Change the gain (volume) of an audio file using ffmpeg.
This function prompts the user for input file details, desired gain value,
and output file details. It then uses ffmpeg to adjust the volume of the
audio file according to the specified gain value.
The function converts the audio to PCM 32-bit floating-point little-endian format
and applies the volume filter to adjust the gain.
Parameters:
None
Returns:
None
Note:
- The input filepath is obtained via user input.
- The gain value (in dB) is obtained via user input and converted to float.
- The output filename (including extension) is obtained via user input.
"""
fileLocation = input("Enter input filepath: ")
gainValue = float(input("Enter gain value (in dB): "))
fileOutputName = input("Enter output filename + extension: ")
(
ffmpeg.input(fileLocation)
.output(
fileOutputName,
acodec="pcm_f32le",
afilter="volume=volume={}".format(gainValue),
)
.run(overwrite_output=True)
)
# subprocess.run(f"ffmpeg -i {fileLocation} -vn -c:a pcm_f32le -filter:a volume={gainValue} {fileOutputName}", shell=True)
def trim_video():
"""
Trim a video file using ffmpeg.
This function prompts the user for input file details, start and end times,
and output file details. It then uses ffmpeg to trim the video according
to the specified start and end times.
The function does not modify the video or audio codecs, only trims the duration.
Parameters:
None
Returns:
None
Note:
- The input filepath is obtained via user input.
- The start time is obtained via user input in the format xx:xx:xx.xxx.
If left blank, it defaults to 00:00:00.000.
- The end time is obtained via user input in the format xx:xx:xx.xxx.
- The output filename (including extension) is obtained via user input.
"""
fileLocation = input("Enter input filepath + extension: ")
startTime = input(
"Enter start time in seconds (format=xx:xx:xx.xxx, default = 00:00:00.000): "
)
endTime = input("Enter end time in seconds (format=xx:xx:xx.xxx): ")
fileOutputName = input("Enter output filename + extension: ")
if not startTime:
startTime = "00:00:00.000"
(
ffmpeg.input(fileLocation, ss=startTime)
.output(
fileLocation.replace(os.path.basename(fileLocation), fileOutputName),
t=endTime,
)
.run(overwrite_output=True)
)
# subprocess.run(f"ffmpeg -i {fileLocation} -ss {startTime} -t {endTime} {fileOutputName}", shell=True)
def generate_thumbnails():
"""
Generate a thumbnail from a video file using ffmpeg.
This function prompts the user for input video file details, timestamp for the thumbnail,
and desired thumbnail size. It then uses ffmpeg to generate a PNG thumbnail from the
specified video at the given timestamp.
Parameters:
None
Returns:
None
Note:
- The input video filepath is obtained via user input.
- The timestamp for the thumbnail is obtained via user input in the format xx:xx:xx.xxx.
- The thumbnail size is obtained via user input in the format widthxheight.
If left blank, it defaults to '720p'.
- The generated thumbnail is saved in the same directory as the input video
with the filename 'thumbnail_001.png'.
"""
fileLocation = input("Enter input video filepath: ")
timeStamp = input("Enter time to generate thumbnail (format=xx:xx:xx.xxx): ")
thumbnailSize = input(
"Enter thumbnail size (format = widthxheight, default = 720p): "
)
if not thumbnailSize:
thumbnailSize = "720p"
# Create output directory if it doesn't exist
# if not os.path.exists(outputDirectory):
# os.makedirs(outputDirectory)
# Generate thumbnails
(
ffmpeg.input(fileLocation, ss=timeStamp)
.output(
os.path.join(os.path.split(fileLocation)[0], "thumbnail_%03d.png"),
vframes=1,
vcodec="png",
)
.run(overwrite_output=True)
)
# subprocess.run(f"ffmpeg -i {fileLocation} -vf thumbnail,scale={thumbnailSize} -r {thumbnailCount} {outputDirectory}/thumbnail_%03d.jpg", shell=True)
def download_video():
"""
Download a video from a given URL using YouTube-DL.
This function prompts the user for a download location and a download link.
It then uses YouTube-DL to download the video in the best available format.
If a custom download location is provided, the video is moved there after download.
Parameters:
None
Returns:
None
Note:
- The download location is obtained via user input. If left blank, the current working directory is used.
- The filename is obtained from video info from the given url. If left blank, the original filename is used.
- The download link is obtained via user input.
- The video is downloaded in the best available format.
- The output template for the downloaded file is set to "%(title)s.%(ext)s".
"""
fileLocation = input(
"Enter video download location (leave blank for CWD) - ignore filename: "
)
fileName = input(
"Enter desired filename (leave blank for default) - ignore extension: "
)
downloadLink = input("Enter download link: ")
ydl_options = {
"format": "bestvideo+bestaudio",
"outtmpl": "%(title)s.%(ext)s",
}
if len(fileLocation) == 0:
fileLocation = os.getcwd()
with YoutubeDL(ydl_options) as ydl:
if os.path.exists(fileLocation):
if len(downloadLink) != 0:
try:
YoutubeDL(ydl_options).download([downloadLink])
except Exception as e:
print("Python import download method failed, using terminal process")
subprocess.run(f"yt-dlp {downloadLink}", shell=True)
return
else:
raise ValueError("Error: Download link not provided.")
# subprocess.run(f"yt-dlp {downloadLink} -o {fileName}", shell=True)
original_filename = ydl.prepare_filename(
ydl.extract_info(downloadLink, download=False)
)
if os.getcwd() == fileLocation and len(str(fileName)) != 0:
shutil.move(
original_filename, f"{fileName}.{original_filename.split('.')[-1]}"
)
print(
f"\nFile renamed to: {fileName}.{original_filename.split('.')[-1]}"
)
elif (
os.getcwd() != fileLocation
and len(fileLocation) > 0
and len(fileName) > 0
):
shutil.move(
original_filename,
f"{os.path.join(fileLocation, fileName)}.{original_filename.split('.')[-1]}",
)
print(
f"\nFile downloaded to: {os.path.join(fileLocation, fileName)}.{original_filename.split('.')[-1]}"
)
else:
shutil.move(
original_filename, os.path.join(fileLocation, original_filename)
)
print(
f"\nFile downloaded to: {os.path.join(fileLocation, fileName)}.{original_filename.split('.')[-1]}"
)
else:
print("\nDownload location not found.")
return original_filename
def exit():
"""
Exits.
This function exits the program.
Parameters:
None. Cuz you're exiting.
Returns:
None. Cuz you've exited.
Note:
- Bye-bye for now.
"""
print("\nExiting...")
sys.exit()
if __name__ == "__main__":
while True:
try:
menu()
except KeyboardInterrupt:
exit()