python
Table des matières
Script pour générer liste de fichiers vidéos
mediainfo
ce logiciel fonctionne pas seulement sur les fichiers mkv
sudo apt-get install mediainfo sudo pip install mediainfo
Bindings python: https://pypi.python.org/pypi/pymediainfo doc: https://pymediainfo.readthedocs.io/en/latest/
sudo apt-get install python3-mediainfodll
sudo pip search mediainfo
sudo pip install pymediainfo sudo apt-get install mediainfo-gui
librairie dans /usr/local/lib/python2.7/dist-packages/pymediainfo
code python dans: /usr/local/lib/python2.7/dist-packages/pymediainfo/init.py
pour ajouter ce dossier au path de python3: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
import sys print(sys.path) ['', '/home/bvandepo/catkin_ws/devel/lib/python2.7/dist-packages', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
ajouter /usr/local/lib/python2.7/dist-packages/
sys.path.append('/usr/local/lib/python2.7/dist-packages/') print(sys.path)
Script de génération de la liste
- walk2.py
#!/usr/bin/python3 # -*- coding: utf-8 -* #B. Vandeportaele 2017 # sudo pip install mediainfo # sudo pip install pymediainfo import os import sys import math #pour trouver pymediainfo sys.path.append('/usr/local/lib/python2.7/dist-packages/') from pymediainfo import MediaInfo #import encodings #import humanize #sudo pip install humanize ############################################################ #pour affichage des tailles from math import log unit_list = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'] unit_list_decimal= [0, 0, 1, 2, 2, 2] def sizeof_fmt(num): """Human friendly file size""" if num > 1: # exponent = min(int(log(num, 1024)), len(unit_list) - 1) exponent = min(int(log(num, 1024)), len(unit_list) - 1) quotient = float(num) / 1024**exponent unit = unit_list[exponent] num_decimals =unit_list_decimal[exponent] format_string = '{:.%sf} {}' % (num_decimals) return format_string.format(quotient, unit) if num == 0: return '0 bytes' if num == 1: return '1 byte' ############################################################ #gérer le tri alphabétique #pour les videos qui sont dans un sous dossier, il faut que ce soit le nom du dossier qui définisse le début du nom pour le tri ############################################################ class Fichier: def __init__(self,name='',location='',collection='',size=0,videocodec='',audiocodec='',width='',height='',duration='',subtitles=''): self.name = name self.cleanedname = name #compute once to speed up the sorting #change , to . to avoid change of column in the csv file self.cleanedname=self.cleanedname.replace(',','.'); self.cleanedname=self.cleanedname.replace('.',' '); self.cleanedname=self.cleanedname.replace('-',' '); self.cleanedname=self.cleanedname.replace('_',' '); self.cleanedname=self.cleanedname.replace('/',' '); self.cleanednameupper = self.cleanedname.upper() #compute once to speed up the sorting self.location =location self.size = size self.collection=collection self.videocodec=videocodec self.audiocodec=audiocodec self.width=width self.height=height self.duration=duration self.subtitles=subtitles def __str__(self): return self.name #redefine "least than <" operator to allow the sorting using uppercase names def __lt__(self,other): if isinstance(other, Fichier): return self.cleanednameupper < other.cleanednameupper else: return NotImplemented ############################################################ #folder_path = "./" folder_path = "/media/freebox/Vidéos/" ma_liste=list() #fich1=Fichier() #fich1.name='efg' #print(fich1.name) #fich2=Fichier() #fich2.name='abc' #print(fich2.name) #ma_liste.append(fich1) #ma_liste.append(fich2) ############################################################ #collection = ['hd','sd','3D'] collection = ['hd','3D'] max_number_of_files= math.inf #décommenter la ligne suivante pour limiter à un certain nombre de fichiers lors des tests #max_number_of_files= 20 number_of_files=0 #mettre à True pour récupérer les infos sur les différents médias (plus long) scanprofond=True #False / True n=1 while n < 16: #while n < 2: directory='/media/HD3TO'+str(n) n=n+1 for col in collection: folder_path = directory+'/'+col+'/' print('------------------------------------------------------------------------') print(folder_path) print('------------------------------------------------------------------------') #https://www.tutorialspoint.com/python/os_walk.htm for root, dirs, files in os.walk(folder_path, topdown=True): # text_file.write(path) # for pat in path: # print(pat) # text_file.write(pat) # for dir in dirs: # print(dir+'/') # text_file.write(dir+'\n') #"Purchase Amount: %s" % TotalAmount) for filename in files: #pour tester un fichier particulier # filename='collider.1080p.multi-www.Zone-Telechargement.com.mkv' number_of_files=number_of_files+1 if number_of_files>max_number_of_files: n=math.inf break #formatage du numéro du fichier sur 5 digits print( '%*d' %(5,number_of_files) +' '+folder_path+filename) #valeurs par défaut audiocodec='' videocodec='' height=0; width=0; duration='' subtitles='' if scanprofond==True: media_info = MediaInfo.parse(folder_path+filename) nbaudiotracks=0 nbaudiotrackstotal=0 nbsubtitletracks=0 nbsubtitletrackstotal=0 #comptage du nombre de pistes for track in media_info.tracks: if track.track_type == 'Audio': nbaudiotrackstotal=nbaudiotrackstotal+1 if track.track_type == 'Text': nbsubtitletrackstotal=nbsubtitletrackstotal+1 #balaye a nouveau for track in media_info.tracks: if track.track_type == 'Video': # print(track.bit_rate, track.bit_rate_mode, track.codec) # videocodec=track.codec+':'+track.bit_rate_mode+':'+track.bit_rate # duration=track.duration #en secondes? #print('type: '+type(track.other_duration).__name__) if type(track.other_duration).__name__!='NoneType': if type(track.other_duration[3]).__name__=='str': duration=track.other_duration[3] #au format '01:46:50.744' #print(duration) if type(track.codec).__name__=='str': videocodec=track.codec+':' if type(track.bit_rate_mode).__name__=='str': videocodec=videocodec+track.bit_rate_mode+':' if type(track.bit_rate).__name__=='int': videocodec=videocodec+str(track.bit_rate) width=track.width height=track.height if track.track_type == 'Text': nbsubtitletracks=nbsubtitletracks+1 subtitles=subtitles+'('+str(nbsubtitletracks)+'/'+str(nbsubtitletrackstotal)+':' #n'ajoute les infos que si elles ont pu être extraites des fichiers if type(track.language).__name__=='str': subtitles=subtitles+track.language subtitles=subtitles+')' if track.track_type == 'Audio': # print(track.bit_rate, track.bit_rate_mode, track.codec) nbaudiotracks=nbaudiotracks+1 audiocodec=audiocodec+'('+str(nbaudiotracks)+'/'+str(nbaudiotrackstotal)+':' #n'ajoute les infos que si elles ont pu être extraites des fichiers if type(track.language).__name__=='str': audiocodec=audiocodec+track.language+':' if type(track.codec).__name__=='str': audiocodec=audiocodec+track.codec+':' if type(track.bit_rate_mode).__name__=='str': audiocodec=audiocodec+track.bit_rate_mode+':' if type(track.bit_rate).__name__=='int': audiocodec=audiocodec+str(track.bit_rate) audiocodec=audiocodec+')' #enlève le dernier caractère en trop et ajoute le nombre de pistes audio # audiocodec=audiocodec[0:len(audiocodec)-1] filename_noext, file_extension = os.path.splitext(filename) file_extension=file_extension.lower() #if filename!='.DS_Store' : if file_extension=='.avi' or file_extension=='.mkv' or file_extension=='.mp4' or file_extension=='.mov' : #debug pour localiser les fichiers dont l'encodage du nom pose problème en utf8 #print(filename[0:6]) #filenameunicode=filename.encode("utf-8") #print(filenameunicode) #print(filename.decode("utf-8")) #print(os.path.join(root, filename)) # text_file.write(filename+'\n') # ma_liste.append(Fichier(filename,0)) completefilename=os.path.join(root, filename) #remove the folder_path from the name filenamewithdir=completefilename[len(folder_path):] #get the file size filesize=os.path.getsize(completefilename) #add to the list ma_liste.append(Fichier(filenamewithdir,folder_path,col,filesize,videocodec,audiocodec,width,height,duration,subtitles)) else: print(filename+ ' ignored\n') ############################################################ #print('liste avant tri') #for f in ma_liste: # print(f) ma_liste.sort() #print('liste après tri') #for f in ma_liste: # print(f) ############################################################ text_file = open("OutputList.csv", "w") #print(ma_liste) text_file.write('type, nom, taille, taille en octets,durée,largeur,hauteur, codecV,codecA,sous-titres, emplacement, adresse\n') for f in ma_liste: # text_file.write(f.name+'\t'+str(f.size)+'\t'+f.location+'\n') # text_file.write(f.collection+','+f.cleanedname+','+str(f.size)+','+f.location+','+f.location+f.name+'\n') # text_file.write(f.collection+','+f.cleanedname+','+sizeof_fmt(f.size)+','+f.location+','+f.location+f.name+'\n') text_file.write(f.collection+','+f.cleanedname+','+sizeof_fmt(f.size)+','+str(f.size)+','+str(f.duration)+','+str(f.width)+','+str(f.height)+','+f.videocodec+','+f.audiocodec+','+f.subtitles+','+f.location+',\''+f.location+f.name+'\'\n') text_file.close()
python.txt · Dernière modification : 2021/02/19 20:20 de 127.0.0.1