Python ffmpeg视频压缩

文章目录

  • 1.项目需求
  • 2. 功能介绍
  • 3. 文件源代码

1.项目需求

质量部有很多视频文件 需要发送给客户,然而,视频文件通常都比较大,需要一种视频转换工具,将视频文件进行压缩,方便传递。并且,不能太影响视频的质量。经过查询,可以通过python以及ffmpeg工具完成视频文件 压缩功能。

2. 功能介绍

ffmpeg是一种开源的视频处理工具,此处我们使用ffmpeg.exe文件对视频文件进行压缩,通过crf参数,来实现文件的压缩功能。crf越小,压缩比越大,视频质量越差。通常来说我们设置18-28的区间,认为认为无法发现太大的视频差异。
ffmpeg需要额外安装,通常来说,将ffmpeg解压到某个路径之后,再将该路径加入到环境变量中。本程序为了方便,直接要求用户将ffmpeg解压到D:\ffmpeg文件夹中。

引用包中的PIL,需要额外安装,其实际名称为Pillow
pip install Pillow

3. 文件源代码

import sys
import os
import zlib
import threading
import platform
from PIL import Image
import tkinter as tk
from tkinter import ttk, HORIZONTAL
from tkinter import filedialog
from tkinter import messagebox
from tkinter import Button, Label, StringVar, IntVar
from tkinter.ttk import Separator
import ctypes
import base64
from icon import img


# 读取合并文件的第一个选择框内容
def askfile1():
    # 打开文件选择器,并筛选出视频文件
    filename = tk.filedialog.askopenfilename(filetypes=[('MP4', '*.mp4 *.avi *.wmv *.mpeg *.3pg *.mov *.m4v *.rmvb'), ('All Files', '*')])
    if not filename:
        entry1.insert(0, '您没有选择任何文件!')
    else:
        entry1.delete(0, "end")     # 选择文件之后,将entry1输入框的内容清空。
        entry1.insert(0, filename)  # 然后将选择的文件全路径插入到entry1输入框中


def compressVedio():
    path = entry1.get()
    outPath = entry2.get()
    if not outPath:
        outPath = "Transfer"
    outPath = outPath + ".mp4"
    compress = r"D:\ffmpeg\bin\ffmpeg.exe -i {} -r 10 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -profile:v baseline -crf 28 -acodec aac -b:a 32k -strict -5 {}".format(path, outPath)
    isRun = os.system(compress)
    # if isRun != 0:
    #     return (isRun, "没有安装ffmpeg")
    print('test')
    pass


if __name__ == "__main__":
    # path = '1.mp4'
    # outpath = '2.mp4'
    # compressVedio(path, outpath)

# if __name__ == "__main__":
    # 以下两句代码用于将windows的任务栏图标设置为跟程序icon一样。
    myappid = "company.product.version"  # 这里可以设置任意文本
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

    # create tkinter instance
    window = tk.Tk()

    # add a title to
    window.title('英纳法重庆公司PDF文件合并/提取及转换工具')

    # set the windows size and cannot resize the window
    window.geometry('560x300')
    window.resizable(0, 0)

    # 将img参数转换成图片,并设置为程序图标
    img_data = base64.b64decode(img)
    with open("pdf.ico", "wb+") as fw:
        fw.write(img_data)
    window.iconbitmap("pdf.ico")
    os.remove("pdf.ico")

    # 定义文件名参数,用于在选择文件之后,显示文件名到后面的输入框中。
    filename1 = StringVar()
    filename2 = StringVar()

    # 读取PDF合并的第一个文件
    btn = tk.Button(window, text='选择', command=askfile1)  # 第一个btn按钮,调用askfile1程序,打开读取文件,并将值传递给entry1
    btn.grid(row=0, column=0)  # 通过grid进行布局,将btn布局到第一行第一列。
    entry1 = tk.Entry(window, textvariable=filename1)  # 定义一个Entry控件,其值为fliename1.
    entry1.grid(row=0, column=1, columnspan=3, ipadx=160)  # 将entry1布局到第一行第二列,跨行3,宽度设置为160.
    entry1.insert(0, "请选择需要压缩的视频文件")
    btn = tk.Button(window, text='选择', command=askfile1)  # 第一个btn按钮,调用askfile1程序,打开读取文件,并将值传递给entry1
    btn.grid(row=1, column=0)  # 通过grid进行布局,将btn布局到第一行第一列。
    entry2 = tk.Entry(window, textvariable=filename2)  # 定义一个Entry控件,其值为fliename1.
    entry2.grid(row=1, column=1, columnspan=3, ipadx=160)  # 将entry1布局到第一行第二列,跨行3,宽度设置为160.
    entry2.insert(0, "请输入转换后的文件名,默认为transfer.mp4")


    # 定义一个BTN,点击执行pdf文件合并功能。
    mergerBtn = tk.Button(window, text='开始视频转换', command=compressVedio)
    mergerBtn.grid(row=2, column=1)

    # 定义一个分割线,用来分开两个功能,sticky指示线的方向
    sep = Separator(window, orient=HORIZONTAL)
    sep.grid(row=3, column=0, columnspan=8, padx=5, pady=10, sticky="EW")


    window.mainloop()
    # pass

你可能感兴趣的:(Python学习记录,ffmpeg,python,开发语言)