利用python+ffmpeg合并B站视频及格式转换
B站客户端下载的视频一般有两种格式:早期的多为blv格式(由flv格式转换而来,音视频轨道在同一文件下)。如今的多为m4s格式,音频轨视频轨分开
以下为利用ffmpeg简单对文件处理,使其转换为大多数播放器能正常播放的mp4格式
前提:已正常安装ffmpeg
import tkinter as tk
from tkinter import filedialog
import os
import tkinter.messagebox
from tkinter import ttk
mainGUI=tk.Tk()
width = 400
height = 400
screenwidth = mainGUI.winfo_screenwidth()
screenheight = mainGUI.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
mainGUI.geometry(alignstr)
mainGUI.title("转码系统")
def browseDialog():
root = tk.Tk()
root.withdraw()
return filedialog.askopenfilename()
def browseDialogTo():
path=browseDialog()
var.set(path)
def browseDialogTo2():
path=browseDialog()
var2.set(path)
def browseDialogTof2():
path=browseDialog()
varf2.set(path)
def browseDialogTof3():
path=browseDialog()
varf3.set(path)
def browseDialogTof4():
path=browseDialog()
varf4.set(path)
tab = ttk.Notebook(mainGUI)
frame0 = tk.Frame(tab)
tab6 = tab.add(frame0, text = "m4s转mp4")
frame1 = tk.Frame(tab)
tab1 = tab.add(frame1, text = "blv,flv转mp4")
frame2 = tk.Frame(tab)
tab2 = tab.add(frame2, text = "提取音频")
frame3 = tk.Frame(tab)
tab3 = tab.add(frame3, text = "音频截取")
tab.pack(expand = True, fill = tk.BOTH)
l1=tk.Label(frame0,text = "请选择视频文件",font=('Arial', 15),width=100,height=2)
l1.pack()
var=tk.StringVar()
var.set("")
e1 = tk.Entry(frame0,textvariable = var, show = None)
e1.pack()
b1=tk.Button(frame0, text='浏览', width=6,
height=2, command=browseDialogTo)
b1.pack()
l2=tk.Label(frame0,text = "请选择音频文件",font=('Arial', 15),width=100,height=2)
l2.pack()
var2=tk.StringVar()
var2.set("")
e2 = tk.Entry(frame0,textvariable = var2, show = None)
e2.pack()
b2=tk.Button(frame0, text='浏览', width=6,
height=2, command=browseDialogTo2)
b2.pack()
l3=tk.Label(frame0,text = "请输入新文件命名",font=('Arial', 15),width=100,height=2)
l3.pack()
e3 = tk.Entry(frame0, show = None)
e3.pack()
def myCMD(path1,path2,name):
ver=os.popen("FFmpeg -i "+path1+" -i "+path2+" -codec copy "+"C:\\Users\\renboyu010214\\Desktop\\outputFile\\"+name+".mp4")
ver.close()
def myCMDmain():
path1=e1.get()
path2=e2.get()
name=e3.get()
myCMD(path1,path2,name)
tkinter.messagebox.showinfo(title='success', message="转码成功!")
b4=tk.Button(frame0, text='确定', width=6,
height=2, command=myCMDmain)
b4.pack()
lf2=tk.Label(frame1,text = "请选择blv或flv文件",font=('Arial', 15),width=100,height=2)
lf2.pack()
varf2=tk.StringVar()
varf2.set("")
ef2 = tk.Entry(frame1,textvariable = varf2, show = None)
ef2.pack()
bf2=tk.Button(frame1, text='浏览', width=6,
height=2, command=browseDialogTof2)
bf2.pack()
lf2_1=tk.Label(frame1,text = "请输入新文件命名",font=('Arial', 15),width=100,height=2)
lf2_1.pack()
ef2_1 = tk.Entry(frame1, show = None)
ef2_1.pack()
def myCMDmainf2():
path1=ef2.get()
name=ef2_1.get()
ver=os.popen("ffmpeg -i "+path1+" "+"C:\\Users\\renboyu010214\\Desktop\\outputFile\\"+name+".mp4")
ver.close()
tkinter.messagebox.showinfo(title='success', message="转码成功!")
bf2=tk.Button(frame1, text='确定', width=6,
height=2, command=myCMDmainf2)
bf2.pack()
lf3=tk.Label(frame2,text = "请选择视频文件",font=('Arial', 15),width=100,height=2)
lf3.pack()
varf3=tk.StringVar()
varf3.set("")
ef3 = tk.Entry(frame2,textvariable = varf3, show = None)
ef3.pack()
bf3=tk.Button(frame2, text='浏览', width=6,
height=2, command=browseDialogTof3)
bf3.pack()
lf3_1=tk.Label(frame2,text = "请输入新文件命名",font=('Arial', 15),width=100,height=2)
lf3_1.pack()
ef3_1 = tk.Entry(frame2, show = None)
ef3_1.pack()
def myCMDmainf3():
path1=ef3.get()
name=ef3_1.get()
ver=os.popen("ffmpeg -i "+path1+" -acodec copy -vn "+"C:\\Users\\renboyu010214\\Desktop\\outputFile\\"+name+".aac")
ver.close()
tkinter.messagebox.showinfo(title='success', message="音频提取成功!")
bf3=tk.Button(frame2, text='确定', width=6,
height=2, command=myCMDmainf3)
bf3.pack()
lf4=tk.Label(frame3,text = "请选择音频/视频文件",font=('Arial', 15),width=100,height=2)
lf4.pack()
varf4=tk.StringVar()
varf4.set("")
ef4 = tk.Entry(frame3,textvariable = varf4, show = None)
ef4.pack()
bf4=tk.Button(frame3, text='浏览', width=6,
height=2, command=browseDialogTof4)
bf4.pack()
lf4_1=tk.Label(frame3,text = "请输入新文件命名(音频后缀.mp3 视频后缀.mp4)",font=('Arial', 15),width=100,height=2)
lf4_1.pack()
ef4_1 = tk.Entry(frame3, show = None)
ef4_1.pack()
lf4_2=tk.Label(frame3,text = "请输入开始时间(xx:xx:xx)",font=('Arial', 15),width=100,height=2)
lf4_2.pack()
ef4_2 = tk.Entry(frame3, show = None)
ef4_2.pack()
lf4_3=tk.Label(frame3,text = "请输入截取时间(xx:xx:xx)",font=('Arial', 15),width=100,height=2)
lf4_3.pack()
ef4_3 = tk.Entry(frame3, show = None)
ef4_3.pack()
def myCMDmainf4():
path1=ef4.get()
name=ef4_1.get()
startTime=ef4_2.get()
time=ef4_3.get()
ver=os.popen("ffmpeg -ss "+startTime+" -t "+time+" -i "+path1+" -vcodec copy -acodec copy "+"C:\\Users\\renboyu010214\\Desktop\\outputFile\\"+name)
ver.close()
tkinter.messagebox.showinfo(title='success', message="音频/视频截取成功!")
bf4=tk.Button(frame3, text='确定', width=6,
height=2, command=myCMDmainf4)
bf4.pack()
mainGUI.mainloop()