如何将两个视频与Python3合并?我尝试了ffmpeg,但我做不到。然后我尝试安装moviepy,但我不能。你能帮我吗?
(其中一个视频只有声音)
我正在使用PyQt5。当我运行时,应用程序关闭。这是退出代码:
Process finished with exit code -1073740791 (0xC0000409)
但是,如果我删除ffmpeg行,效果很好。
我尝试的第一种方式:
video = ffmpeg.input('videoo.mp4')
audio = ffmpeg.input('voicee.mp4')
ffmpeg.output(video, audio, 'out.mp4').run()
我尝试的第二种方式:
ffmpeg.concat(ffmpeg.input('videoo.mp4'), ffmpeg.input('voicee.mp4'), v=1, a=1).output('vdeo.mp4').run()
我尝试的第三种方式:
subprocess.run("ffmpeg -i videoo.mp4 -i voicee.webm -c:v copy -c:a copy output.mp4")
答案 0 :(得分:0)
由于OP没有提供MRE,所以很难知道问题出在哪里,所以我的答案集中在提供仅实现功能的代码上。
在这种情况下,转换非常耗时,因此必须在辅助线程中执行:
import sys
import threading
import ffmpeg
from PyQt5 import QtCore, QtWidgets
class Converter(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
def concatenate(self, inputs, output):
threading.Thread(
target=self._concatenate, args=(inputs, output), daemon=True
).start()
def _concatenate(self, inputs, output):
self.started.emit()
ffmpeg.concat(*[ffmpeg.input(i) for i in inputs]).output(output).run()
self.finished.emit()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.input_1_le = QtWidgets.QLineEdit()
self.input_2_le = QtWidgets.QLineEdit()
self.output_le = QtWidgets.QLineEdit()
self.load_1_btn = QtWidgets.QPushButton("Select 1 input")
self.load_2_btn = QtWidgets.QPushButton("Select 2 input")
self.load_3_btn = QtWidgets.QPushButton("Select output")
self.start_btn = QtWidgets.QPushButton("Start")
lay = QtWidgets.QGridLayout(self)
lay.addWidget(QtWidgets.QLabel("Input 1"), 0, 0)
lay.addWidget(self.input_1_le, 0, 1)
lay.addWidget(self.load_1_btn, 0, 2)
lay.addWidget(QtWidgets.QLabel("Input 2"), 1, 0)
lay.addWidget(self.input_2_le, 1, 1)
lay.addWidget(self.load_2_btn, 1, 2)
lay.addWidget(QtWidgets.QLabel("Output"), 2, 0)
lay.addWidget(self.output_le, 2, 1)
lay.addWidget(self.load_3_btn, 2, 2)
lay.addWidget(self.start_btn, 3, 0, 1, 3)
self.converter = Converter()
self.load_1_btn.clicked.connect(self.load_input_1)
self.load_2_btn.clicked.connect(self.load_input_2)
self.load_3_btn.clicked.connect(self.load_output)
self.start_btn.clicked.connect(self.start)
self.converter.started.connect(lambda: self.start_btn.setEnabled(False))
self.converter.finished.connect(lambda: self.start_btn.setEnabled(True))
@QtCore.pyqtSlot()
def load_input_1(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self,
self.tr("Select Video"),
QtCore.QDir.homePath(),
self.tr("Video Files (*.mp4)"),
)
if filename:
self.input_1_le.setText(filename)
@QtCore.pyqtSlot()
def load_input_2(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self,
self.tr("Select Video"),
QtCore.QDir.homePath(),
self.tr("Video Files (*.mp4)"),
)
if filename:
self.input_2_le.setText(filename)
@QtCore.pyqtSlot()
def load_output(self):
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
self,
self.tr("Select Video"),
QtCore.QDir.homePath(),
self.tr("Video Files (*.mp4)"),
)
if filename:
self.output_le.setText(filename)
@QtCore.pyqtSlot()
def start(self):
self.converter.concatenate(
[self.input_1_le.text(), self.input_2_le.text()], self.output_le.text()
)
def main():
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()