'ffmpeg-python compression code not compressing. no error message just not changing file size
I'm really lost, I'm not sure what the issue is. The file is processing as the program takes longer to run the longer the video is, but it just does nothing. I don't think the compressed file is being output for some reason, and I cant figure it out. Help appreciated, but it may be hard as there is no error message of any kind.
##import section:this part is where I import all of the modules I will use
import tkinter
import shutil
from tkinter import filedialog
import os
import random
import ffmpeg
def fileSelect(): #start of fileSelect function
global startingLocation #declares startingLocation as global variable
global originalName #declares originalName as global variable
global fileType #declares fileType as global variable
startingLocation = filedialog.askopenfilename(initialdir="/", title="Select file", #tkinter function that opens file explorer, lets user select file saves the file path as a variable
filetypes=(("video files", "*.mp4"),("images", "*.jpg*")))
originalName = os.path.basename(startingLocation) #os function that gets the actaul file name from the path string
print (originalName) #print statement to check if originalName has been found
fileType = startingLocation.split('.') #splits original name where any full stop in found and saves array as variable
fileType = fileType[-1] #changes variable to have the str value of the final item in the array; the file type
fileType = '.' + fileType #adds fullstop to the start of the file type so i dont have to repeatedly do it
print (fileType) #print statement to check file type is found correctly
def outputSelect(): #start of outputSelect function
global outputLocation #declares outputLocation as global variable
outputLocation = filedialog.askdirectory(initialdir="/", title="Select folder") #tkinter function that opens file explorer, lets the user select of folder as saves the folder path as a variable
def fileNewName(): #start of fileNewName function
global customName #declares customName as global variable
customName = input("Enter the end name of your file ") #simple code assigning user input to the custom name vairable
if customName and customName.strip(): #if statement to check if user name is blank
customName = customName + fileType #add the fileType onto the end of the custom name
else: #else
list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1','2', '3', '4', '5', '6', '7', '8', '9','0']
customGen = '' #declares customGen as a string (36 characters)
for x in range (0,8): #loop for 8 to make custom name 8 letters long
x = random.randint(0,35) #selects a random number the length of the array
customGen = customGen + list[x] #adds random character from list to the string
customName = customGen + fileType #if user name blank, changes variable to custom name
print(customName) #print statement to check the custom name
def compress(): #start of compress function
fileSelect() #calls the fileSelect function
outputSelect() #calls the outputSelect function
fileNewName() #calls the fileNewName function
global src #declares src as a global variable
global dst #declares dst as a global variable
src = startingLocation #assigns startingLocation str as src, so the shutil module is able to use it in a cleaner way
dst = outputLocation #assigns outputLocation dst as src, so the shutil module is able to use it in a cleaner way
shutil.copy(src, dst) #shutil command that copies the file from src to dst
src = outputLocation + '/' + originalName #reassigns src as the location of the file copy
dst = outputLocation + '/' + customName #reassigns dst as the location of the file copy but with a new name
shutil.move(src,dst) #moves and renames the file
print(dst)
def compressVideo(videoFullPath, videoFileName, targetSize):
minAudioBitrate = 32000
maxAudioBitrate = 256000
probe = ffmpeg.probe(videoFullPath)
duration = float(probe['format']['duration'])
audioBitrate = float(next((s for s in probe['streams'] if s['codec_type'] == 'audio'), None)['bit_rate'])
targetTotalBitrate = (targetSize * 1024 * 8) / (1.073741824 * duration)
if 10 * audioBitrate > targetTotalBitrate:
audioBitrate = targetTotalBitrate / 10
if audioBitrate < minAudioBitrate < targetTotalBitrate:
audioBitrate = minAudioBitrate
elif audioBitrate > maxAudioBitrate:
audioBitrate = maxAudioBitrate
videoBitrate = targetTotalBitrate - audioBitrate
i = ffmpeg.input(videoFullPath)
ffmpeg.output(i,'NUL',
**{'c:v': 'libx264', 'b:v': videoBitrate, 'pass': 1, 'f': 'null'}
).overwrite_output().run()
ffmpeg.output(i, videoFileName,
**{'c:v': 'libx264', 'b:v': videoBitrate, 'pass': 2, 'c:a': 'aac', 'b:a': audioBitrate, 'f' :'mp4'}
).overwrite_output().run()
compress()
compressVideo(dst, customName + 'compressed', 50 * 1000)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|