[Python] TypeError: a float is required greška

C, C++, Java, PHP, Ruby...

Moderator/ica: Moderatori/ce

Pravila foruma
U naslovu teme unutar uglatih zagrada navesti o kojem jeziku je riječ. Primjer: [Java]
Odgovori
qwxy
Postovi: 121
Pridružen/a: 21 srp 2012, 00:36
Spol: M
OS: Arch Compiz|LXQt Debian

[Python] TypeError: a float is required greška

Post Postao/la qwxy »

Ovdje imam manji program koji sam napisao kao vježbu, jer do sada nikada nisam učio python. Prvi je složeniji program koji pišem u pythonu. Nije mi jasno kako se riješiti ove greške:

Kod: Označi sve

File "converter.py", line 124, in <module>
    b_video = vbitrate_askif()
  File "converter.py", line 49, in vbitrate_askif
    b_video = str(vbitrate_calculate())
  File "converter.py", line 17, in vbitrate_calculate
    filebitrate = vidstat.st_size / int(math.floor(vidlength))
TypeError: a float is required
Hvala unaprijed. Također, sve kritike na stil su dobrodošle... ili možda ipak da se držim web dizajna :)

Kod: Označi sve

if ('h' in url) and ('t' in url) and ('p' in url) and (':' in url) and ('/' in url):
Što se ovoga tiče, znam bolji način.

Kod: Označi sve

#!/usr/bin/env python

import os, sys, subprocess, math

audiopf = "audioprep"
vf = "video.mp4"
addparm = ""

def vbitrate_calculate():
	vidlengthp1 = subprocess.Popen(["ffprobe", "-loglevel", "error", "-show_streams", vf], stdout=subprocess.PIPE)
	vidlengthp2 = subprocess.Popen(["grep", "duration"], stdin=vidlengthp1.stdout, stdout=subprocess.PIPE)
	vidlengthp3 = subprocess.Popen(["cut", "-f2", "-d="], stdin=vidlengthp2.stdout, stdout=subprocess.PIPE)
	vidlengtht = vidlengthp3.communicate()[0]
	vidlengthstr = vidlengtht.decode("utf-8")
	vidlength = vidlengthstr.splitlines()[1]
	vidstat = os.stat(vf)
	filebitrate = vidstat.st_size / int(math.floor(vidlength))
	vbitrate = filebitrate - b_audio
	return vbitrate

def remove_o_files():
	afexists = os.path.isfile(afilename)
	if afexists == True:
		os.remove(afilename)
	afiexists = os.path.isfile(afilename+".part")
	if afiexists == True:
		os.remove(afilename+".part")
	afiiexists = os.path.isfile(rstr)
	if afiiexists == True:
		os.remove(rstr)
	afiiiexists = os.path.isfile(fafilename)
	if afiiiexists == True:
		os.remove(fafilename)
	vfexists = os.path.isfile(vfilename)
	if vfexists == True:
		os.remove(vfilename)
	vfiexists = os.path.isfile(vfilename+".part")
	if vfiexists == True:
		os.remove(vfilename+".part")
	vfiiexists = os.path.isfile(vf)
	if vfiiexists == True:
		os.remove(vf)

def vbitrate_askif():
	b_askif = input("Do you want to input the target video stream bitrate yourself of let it be calculated? (y/n) ")
	if b_askif == "y":
		b_video = input("Input target bitrate: ")
	else:
		b_video = str(vbitrate_calculate())
	if b_video[-1:]!="k":
			b_video = b_video + "k"

	return b_video

url = input("URL: ")
if ('h' in url) and ('t' in url) and ('p' in url) and (':' in url) and ('/' in url):
	os.system("youtube-dl -F " + url)
else:
	print("Incorrect input")
	sys.exit()

audiohack = input("192k audio? (y/n) ")
if audiohack=="y":
	audiofile = 22
	b_audio=192
else:
	audiofile = 140
	b_audio=128

videofile = input("Video number (mp4): ")

vfilenamep = subprocess.Popen(["youtube-dl", "-f", str(videofile), url, "--get-filename"], stdout=subprocess.PIPE)
vfilenamet = vfilenamep.communicate()
vfilenamestr = vfilenamet[0]
vfilename = vfilenamestr[:-1].decode("utf-8")
vextstr = vfilenamestr[-5:]
vext = vextstr[:-1].decode("utf-8")
if vext!=".mp4":
	print ("Unsupported file container format")
	sys.exit()

afilenamep = subprocess.Popen(["youtube-dl", "-f", str(audiofile), url, "--get-filename"], stdout=subprocess.PIPE)
afilenamet = afilenamep.communicate()
afilenamestr = afilenamet[0]
afilename = afilenamestr[:-1].decode("utf-8")
aextstr = afilenamestr[-5:]
aext = aextstr[:-1].decode("utf-8")
rstr = audiopf + str(aext)
fafilename = "audio" + str(aext)

remove_o_files()

os.system("youtube-dl -f " + str(videofile) + " " + url)
os.rename(vfilename, vf)
os.system("youtube-dl -f " + str(audiofile) + " " + url)
os.rename(afilename, rstr)	
os.system("ffmpeg -i " + rstr + " -vn -c:a copy " + '"' + fafilename + '"')
if audiohack=="y":
	os.remove(rstr)

print("Files have been downloaded")
print("Select one of the operations below:")
print("0 - quit")
print("1 - stream copy/join audio and video files")
print("2 - audio: libopus; video: libtheora")
print("3 - audio: flac; video: libtheora")
print("4 - audio: mp3; video: libxvid")

selectoperation = input("Option: ")

if selectoperation=="0":
	sys.exit()
else:
	outfilename = input("Enter output file name (without extension): ")
	if selectoperation=="1":
		os.system("ffmpeg -i " + fafilename + " -i " + vf + " -c:a copy -c:v copy " + outfilename + ".mp4")
		print("Output file is saved as " + outfilename + ".mp4")
	if selectoperation=="2":
		mqscale = input("Do you want the best quality video, regardless of file size? (y/n) ")
		if mqscale=="y":
			os.system("ffmpeg -i " + fafilename + " -i " + vf + " -c:a libopus -b:a " + str(b_audio) + "k -c:v libtheora -qscale:v 10 " + addparm + " " + outfilename + ".ogv")
			
		else:
			b_video = vbitrate_askif()
			os.system("ffmpeg -i " + fafilename + " -i " + vf + " -c:a libopus -b:a " + str(b_audio) + "k -c:v libtheora -b:v " + b_video + addparm + " " + outfilename + ".ogv")
		print("Output file is saved as " + outfilename + ".ogv")

remove_o_files()
Zadnja izmjena: qwxy, dana/u 02 srp 2014, 05:31, ukupno mijenjano 2 puta.
Avatar
stefan
Moderator
Postovi: 4366
Pridružen/a: 28 sij 2009, 18:46
Spol: M
OS: openSUSE Leap KDE

Re: [Python] TypeError: a float is required greška

Post Postao/la stefan »

Nisam pokretao kod ali očito je da imaš grešku u liniji 17

Matematičkoj funkciji funkciji za zaokruživanje "na više" floor kao argument daješ string, a floor mora primati broj.

Kod: Označi sve

filebitrate = vidstat.st_size / int(math.floor(vidlength))
vidlength dobiješ sa split i valjda bi trebao sadržati broj?
Ako da onda ga prvo pretvori u float:

Kod: Označi sve

filebitrate = vidstat.st_size / int(math.floor(float(vidlength)))
ali bolje bi ti to bilo raspisati u više linija:

Kod: Označi sve

vl = float(vidlength)
vl =  int(math.floor(vl))
filebitrate = vidstat.st_size / vl
Nadam se da moj odgovor pomaže.. ;)
Like some other animals, the gecko can perform a neat trick when threatened by a predator: it can amputate its own tail. The dropped tail serves to distract the predator, and by losing it, the lizard can run faster.
qwxy
Postovi: 121
Pridružen/a: 21 srp 2012, 00:36
Spol: M
OS: Arch Compiz|LXQt Debian

Re: [Python] TypeError: a float is required greška

Post Postao/la qwxy »

Znao sam da je neka glupost u pitanju. :) Hvala.
I da, sve ostalo se čini uredu.
Odgovori