Skip to content

Commit 6b76fb4

Browse files
authored
Update FFmpeg2Discord.py
1 parent fd8333f commit 6b76fb4

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/FFmpeg2Discord.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from ffmpeg_progress_yield import FfmpegProgress
22
from PyQt5 import QtCore, QtGui, QtWidgets
3-
from moviepy.editor import VideoFileClip
43
from ui import Ui_MainWindow
54
from platform import system
65
import subprocess
@@ -88,31 +87,47 @@ def confirm(self):
8887
print(self.startTime)
8988

9089
# Determine bitrate based on length of video.
91-
videoLength = VideoFileClip(filePath).duration
90+
command = ["./tools/ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filePath]
91+
output = subprocess.check_output(command)
92+
videoLength = float(output)
9293
if self.startTime != "" and self.endTime == "":
9394
videoLength = videoLength - self.convertTimeToSeconds(self.startTime)
94-
ffmpegCommand.extend(["-ss", self.startTime])
95+
ffmpegCommand.insert(1, "-ss")
96+
ffmpegCommand.insert(2, self.startTime)
9597

9698
elif self.startTime == "" and self.endTime != "":
9799
videoLength = self.convertTimeToSeconds(self.endTime)
98-
ffmpegCommand.extend(["-to", self.endTime])
100+
ffmpegCommand.insert(1, "-ss")
101+
ffmpegCommand.insert(2, self.endTime)
99102

100103
elif self.startTime != "" and self.endTime != "":
101104
videoLength = self.convertTimeToSeconds(self.endTime) - self.convertTimeToSeconds(self.startTime)
102-
ffmpegCommand.extend(["-ss", self.startTime, "-to", self.endTime])
105+
ffmpegCommand.insert(1, "-ss")
106+
ffmpegCommand.insert(2, self.startTime)
107+
ffmpegCommand.insert(3, "-to")
108+
ffmpegCommand.insert(4, self.endTime)
103109

104110
bitrate = (self.targetFileSize/videoLength)-self.audioBitrate
105111
bitrate = int(bitrate)
106112

107113
fileSize = os.path.getsize(filePath)
108114

109-
videoFPS = VideoFileClip(filePath).fps
115+
command = ["./tools/ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=r_frame_rate", "-of", "default=noprint_wrappers=1:nokey=1", filePath]
116+
output = subprocess.check_output(command, universal_newlines=True)
117+
fps_str = output.strip()
118+
numerator, denominator = map(int, fps_str.split('/'))
119+
videoFPS = float(numerator) / float(denominator)
110120
if videoFPS > 30 and fileSize > 50000000:
111121
ffmpegCommand.extend(["-r", "30"])
112122

113-
clip = VideoFileClip(filePath)
114-
width = clip.size[0]
115-
height = clip.size[1]
123+
command = ['./tools/ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=s=x:p=0', filePath]
124+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
125+
output, _ = process.communicate()
126+
127+
# Parse the output to get width and height
128+
dimensions = output.decode().strip().split('x')
129+
width = int(dimensions[0])
130+
height = int(dimensions[1])
116131
if (width > 720 or height > 720) and fileSize > 100000000:
117132
if width > height:
118133
ffmpegCommand.extend(["-vf", "scale='trunc(oh*a/2)*2:720'"])
@@ -135,9 +150,6 @@ def confirm(self):
135150
ffmpegCommand.extend(["-preset", "ultrafast", "-c:v", "libx264"])
136151
ffmpegCommand2 = ffmpegCommand.copy()
137152

138-
ffmpegCommand.extend(["-pass", "1"])
139-
ffmpegCommand2.extend(["-pass", "2"])
140-
141153
progressPercent = 2
142154
progressPercent2 = 2
143155
progressPercent3 = 50
@@ -146,9 +158,6 @@ def confirm(self):
146158
ffmpegCommand.extend(["-preset", "veryslow", "-c:v", "libx264"])
147159
ffmpegCommand2 = ffmpegCommand.copy()
148160

149-
ffmpegCommand.extend(["-pass", "1"])
150-
ffmpegCommand2.extend(["-pass", "2"])
151-
152161
progressPercent = 5
153162
progressPercent2 = (5/4)
154163
progressPercent3 = 20
@@ -164,14 +173,14 @@ def confirm(self):
164173

165174
videoProgress += 1
166175
self.label.setText(str(videoProgress) + "/" + str(numOfVideos))
167-
ffmpegCommand.extend(["-ac", "2", "-map", "0:v", "-b:v", str(bitrate), "-b:a", str(self.audioBitrate), "-c:a", "libopus", "-f", "mp4", str(trash), "-y"])
176+
ffmpegCommand.extend(["-ac", "2", "-map", "0:v", "-b:v", str(bitrate), "-b:a", str(self.audioBitrate), "-c:a", "libopus", "-pass", "1", "-f", "mp4", str(trash), "-y"])
168177
print(ffmpegCommand)
169178
ff = FfmpegProgress(ffmpegCommand)
170179
for progress in ff.run_command_with_progress():
171180
self.progressBar.setValue(int(progress / progressPercent))
172181
print(progress)
173182

174-
ffmpegCommand2.extend(["-ac", "2", "-map", "0:v", "-b:v", str(bitrate), "-b:a", str(self.audioBitrate), "-c:a", "libopus", outputFile, "-y"])
183+
ffmpegCommand2.extend(["-ac", "2", "-map", "0:v", "-b:v", str(bitrate), "-b:a", str(self.audioBitrate), "-c:a", "libopus", "-pass", "2", outputFile, "-y"])
175184
ff = FfmpegProgress(ffmpegCommand2)
176185
for progress in ff.run_command_with_progress():
177186
self.progressBar.setValue(int(progress / progressPercent2) + progressPercent3)

0 commit comments

Comments
 (0)