-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVideo2Discord.py
112 lines (86 loc) · 4.56 KB
/
Video2Discord.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
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import os, time, json, winsound, shutil, sys
from tkinter import filedialog
FILE_SIZE_LIMIT = 10 # MiB
ffmpeg = "ffmpeg"
ffprobe = "ffprobe"
if os.path.isfile("FFmpeg/bin/ffmpeg.exe") == True and os.path.isfile("FFmpeg/bin/ffprobe.exe") == True:
print("Using bundled FFmpeg")
ffmpeg = "FFmpeg\\bin\\ffmpeg.exe"
ffprobe = "FFmpeg\\bin\\ffprobe.exe"
if (str(shutil.which("ffmpeg")) == "None" and os.path.isfile("FFmpeg/bin/ffmpeg.exe") == False) or (str(shutil.which("ffprobe")) == "None" and os.path.isfile("FFmpeg/bin/ffprobe.exe") == False):
print("FFmpeg is not installed in your computer. Please download FFmpeg and add it to PATH. If you need a guide on how to install it, you can watch a tutorial like this one: https://youtu.be/IECI72XEox0")
time.sleep(1)
input("Press the enter key to exit ")
sys.exit()
if not os.path.isdir("Videos"):
os.makedirs("Videos")
input_video_list = filedialog.askopenfilenames()
for input_video in input_video_list:
output_video = os.path.basename(input_video)
output_video = ".".join(output_video.split(".")[:-1])
if os.path.isfile(f"Videos/{output_video}.webm") == True:
suffix = 1
while os.path.isfile(f"Videos/{output_video} ({suffix}).webm") == True:
suffix += 1
output_video = f"{output_video} ({suffix})"
os.system(f"{ffprobe} -hide_banner -of json -select_streams v -show_format -show_streams -i \"{input_video}\" -o \"temp.json\"")
with open("temp.json", "r", encoding="utf-8") as openfile:
video_data = json.load(openfile)
duration = float(video_data["format"]["duration"])
width = int(video_data["streams"][0]["width"])
height = int(video_data["streams"][0]["height"])
total_kb = FILE_SIZE_LIMIT * 1024 * 8
video_bitrate = (total_kb / duration)
if video_bitrate >= 500:
video_bitrate = video_bitrate * 0.99
if video_bitrate < 500:
video_bitrate = video_bitrate - 5
if width >= height:
video_res = "-vf scale=-1:1080"
if width < height:
video_res = "-vf scale=1080:-1"
video_fps = 60
audio_channel = ""
audio_bitrate = 128
if video_bitrate > 6000:
video_bitrate = 6000
if video_bitrate < 3000:
video_res = video_res.replace("1080", "720")
audio_bitrate = 96
if video_bitrate < 1500:
video_res = video_res.replace("720", "540")
audio_bitrate = 64
if video_bitrate < 750:
video_res = video_res.replace("540", "480")
if video_bitrate < 450:
video_res = video_res.replace("480", "360")
video_fps = 30
audio_bitrate = 48
if video_bitrate < 300:
audio_bitrate = 32
if video_bitrate < 232:
video_res = video_res.replace("360", "240")
audio_bitrate = 24
audio_channel = "-ac 1 -apply_phase_inv 0"
if video_bitrate < 140:
audio_bitrate = 14
if video_bitrate < 90:
video_res = video_res.replace("240", "144")
if width >= height:
if height <= int(video_res.replace("-vf scale=-1:", "")):
video_res = ""
if width < height:
if width <= int(video_res.replace("-vf scale=", "").replace(":-1", "")):
video_res = ""
os.system(f"{ffmpeg} -hide_banner -loglevel warning -stats -i \"{input_video}\" -fpsmax {video_fps} {video_res} -c:v libvpx-vp9 -pix_fmt yuv420p -g {video_fps * 20} -keyint_min {video_fps * 2} -lag-in-frames 25 -auto-alt-ref 1 -arnr-maxframes 7 -arnr-strength 4 -enable-tpl 1 -deadline good -cpu-used 4 -row-mt 1 -b:v {video_bitrate - audio_bitrate}k -pass 1 -an -y -f null NUL && ^{ffmpeg} -hide_banner -loglevel warning -stats -i \"{input_video}\" -fpsmax {video_fps} {video_res} -c:v libvpx-vp9 -pix_fmt yuv420p -g {video_fps * 20} -keyint_min {video_fps * 2} -lag-in-frames 25 -auto-alt-ref 1 -arnr-maxframes 7 -arnr-strength 4 -enable-tpl 1 -deadline good -cpu-used 4 -row-mt 1 -b:v {video_bitrate - audio_bitrate}k -pass 2 -c:a libopus {audio_channel} -b:a {audio_bitrate}k \"Videos/{output_video}.webm\"")
print("")
if os.path.isfile("temp.json") == True:
os.remove("temp.json")
if os.path.isfile("ffmpeg2pass-0.log") == True:
os.remove("ffmpeg2pass-0.log")
if input_video_list:
winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
time.sleep(1)