from moviepy.editor import VideoFileClip from PyQt5.QtWidgets import QFileDialog, QApplication import sys import os import subprocess def choose_file(): fname = QFileDialog.getOpenFileName(None, 'Open file', 'E:\Picture\MIUI',"Video files (*.mp4 *.avi)") return fname[0] def main(): app = QApplication(sys.argv) fname = choose_file() if fname: clip = VideoFileClip(fname).resize(0.5).subclip(t_start=1, t_end=2).resize(0.5) clip.write_gif("movie.gif",fps=30) subprocess.call(["convert", "movie.gif[0]", "movie.gif"]) if __name__ == '__main__': main() 如何保存GIf到原视频的目录
时间: 2024-02-28 20:54:48 浏览: 64
0.zip_pyqt jpg预览_pyqt5_pyqt5打开_pyqt打开文件夹_qfiledialog
你可以使用 `os.path.dirname()` 函数获取原视频的目录,然后将生成的 GIF 文件移动到该目录下,代码示例如下:
```python
import os
def main():
app = QApplication(sys.argv)
fname = choose_file()
if fname:
clip = VideoFileClip(fname).resize(0.5).subclip(t_start=1, t_end=2).resize(0.5)
clip.write_gif("movie.gif", fps=30)
subprocess.call(["convert", "movie.gif[0]", "movie.gif"])
original_dir = os.path.dirname(fname)
gif_path = os.path.join(original_dir, "movie.gif")
os.rename("movie.gif", gif_path)
if __name__ == '__main__':
main()
```
这样会将生成的 GIF 文件重命名为 `movie.gif` 并将其移动到原视频的目录下。
阅读全文