Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Command line use of 'ffprobe':
- #
- # ffprobe -loglevel quiet -print_format json \
- # -show_format -show_streams \
- # video-file-name.mp4
- #
- # man ffprobe # for more information about ffprobe
- #
- import sys
- import subprocess
- import json
- import time
- import os
- import win32com.client
- import pipes
- import platform
- def check_ffprobe():
- try:
- with open(os.devnull, 'w') as tempf:
- subprocess.check_call(["ffprobe", "-h"], stdout=tempf, stderr=tempf)
- except FileNotFoundError:
- raise IOError('ffprobe not found.')
- def probe(vid_file_path):
- if platform.system() == 'Windows':
- cmd = ["ffprobe", "-loglevel", "quiet", "-print_format", "json", "-show_format", "-show_streams", vid_file_path]
- else:
- cmd = ["ffprobe -show_streams " + pipes.quote(vid_file_path)]
- with subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) as proc:
- out, err = proc.communicate()
- return json.loads(out)
- # p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- # out, err = p.communicate()
- # p.stdout.close()
- # p.stderr.close()
- # return json.loads(out)
- def duration(vid_file_path):
- # Video's duration in seconds, return a float number
- _json = probe(vid_file_path)
- if 'format' in _json:
- if 'duration' in _json['format']:
- return float(_json['format']['duration'])
- if 'streams' in _json:
- # commonly stream 0 is the video
- for s in _json['streams']:
- if 'duration' in s:
- return float(s['duration'])
- # if everything didn't happen,
- # we got here because no single 'return' in the above happen.
- raise NameError('I found no duration in file ' + vid_file_path)
- def seconds_to_d_h_m_s(temp_seconds):
- i_sekunden = divmod(temp_seconds, 60)[1]
- i_minuten1 = divmod(temp_seconds, 60)[0]
- i_stunden1 = divmod(i_minuten1, 60)[0]
- i_minuten2 = divmod(i_minuten1, 60)[1]
- i_tage = divmod(i_stunden1, 24)[0]
- i_stunden2 = divmod(i_stunden1, 24)[1]
- if int(i_tage) > 0:
- diff_time = str(int(i_tage)) + " Tage, " + str(int(i_stunden2)) + " Stunden, " + str(
- int(i_minuten2)) + " Minuten, " + str(round(i_sekunden)) + " Sekunden"
- elif int(i_stunden2) > 0:
- diff_time = str(int(i_stunden2)) + " Stunden, " + str(int(i_minuten2)) + " Minuten, " + str(
- round(i_sekunden)) + " Sekunden"
- elif int(i_minuten2) > 0:
- diff_time = str(int(i_minuten2)) + " Minuten, " + str(round(i_sekunden)) + " Sekunden"
- else:
- diff_time = str(round(i_sekunden)) + " Sekunden"
- return diff_time
- def has_video_endings(file):
- media_endings = [".mp4", ".mkv", ".wmv", ".mov"]
- return [file for ending in media_endings if file.lower().endswith(ending)]
- def has_link_endings(file):
- link_endings = [".lnk", ".url"]
- return [file for ending in link_endings if file.lower().endswith(ending)]
- def resolve_link(file):
- if not has_link_endings(file):
- return False
- shell = win32com.client.Dispatch("WScript.Shell")
- shortcut = shell.CreateShortCut(file)
- real_file = shortcut.Targetpath
- print("Link: " + file + " ------- > " + real_file)
- return file_or_path(real_file)
- def file_counting(video_file):
- global total_duration
- try:
- file_duration = duration(video_file)
- except NameError as err:
- print(err)
- return False
- print(file_duration)
- total_duration += file_duration
- zaehler_add(0)
- return True
- def zaehler_add(mode):
- global zaehler
- global total_zaehler
- if mode == 0:
- zaehler += 1
- total_zaehler += 1
- if mode == 1:
- total_zaehler += 1
- def progress_file(file):
- if has_video_endings(file):
- return file_counting(file)
- is_link = resolve_link(file)
- if not is_link:
- print("This file has no video ending and is not a link")
- zaehler_add(1)
- return False
- def progress_path(path):
- # walk should not go down to symlinks
- for (dirpath, dirnames, filenames) in os.walk(path):
- if len(filenames) > 0:
- # get_files[dirpath] = filenames
- for filename in filenames:
- print(filename + " ------- > im Verzeichnis: " + dirpath)
- file_or_path(os.path.join(dirpath, filename))
- # for y in get_files:
- # for x in get_files[y]:
- # print(x + " ------- > im Verzeichnis: " + y)
- # file_or_path(os.path.join(y, x))
- return True
- def file_or_path(x):
- if type(x) != str or x is False:
- return False
- if not os.path.exists(x): # doppelt?
- print("Das exististert nicht: " + x)
- return False
- if os.path.isfile(x):
- progress_file(x)
- elif os.path.isdir(x):
- progress_path(x)
- else:
- print("Das ist weder eine Datei, noch ein Pfad")
- return False
- return True
- if __name__ == "__main__":
- if len(sys.argv) > 1:
- files_paths = sys.argv[1:]
- else:
- files_paths = []
- raw_input = input("Gib das Verzeichnis an: ")
- files_paths.append(raw_input)
- while True:
- raw_input = input("Noch mehr Verzeichnisse? (y/n): ").strip()
- if raw_input == "y":
- files_paths.append(input("Gib das zusätzliche Verzeichnis an: "))
- else:
- break
- print(files_paths)
- # get_files = {}
- zaehler = 0
- total_zaehler = 0
- total_duration = 0
- for item in files_paths:
- # get_files = {}
- file_or_path(item)
- print(total_duration)
- print(seconds_to_d_h_m_s(total_duration))
- print(str(zaehler) + " / " + str(total_zaehler))
- while input("Beenden?: y ") != "y":
- time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement