Advertisement
Guest User

Untitled

a guest
Apr 26th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.85 KB | None | 0 0
  1. #
  2. # Command line use of 'ffprobe':
  3. #
  4. # ffprobe -loglevel quiet -print_format json \
  5. #         -show_format    -show_streams \
  6. #         video-file-name.mp4
  7. #
  8. # man ffprobe # for more information about ffprobe
  9. #
  10. import sys
  11.  
  12. import subprocess
  13. import json
  14. import time
  15. import os
  16. import win32com.client
  17.  
  18. import pipes
  19. import platform
  20.  
  21.  
  22. def check_ffprobe():
  23.     try:
  24.         with open(os.devnull, 'w') as tempf:
  25.             subprocess.check_call(["ffprobe", "-h"], stdout=tempf, stderr=tempf)
  26.     except FileNotFoundError:
  27.         raise IOError('ffprobe not found.')
  28.  
  29.  
  30. def probe(vid_file_path):
  31.     if platform.system() == 'Windows':
  32.         cmd = ["ffprobe", "-loglevel", "quiet", "-print_format", "json", "-show_format", "-show_streams", vid_file_path]
  33.     else:
  34.         cmd = ["ffprobe -show_streams " + pipes.quote(vid_file_path)]
  35.  
  36.     with subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) as proc:
  37.         out, err = proc.communicate()
  38.         return json.loads(out)
  39.  
  40.     # p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  41.     # out, err = p.communicate()
  42.     # p.stdout.close()
  43.     # p.stderr.close()
  44.     # return json.loads(out)
  45.  
  46.  
  47. def duration(vid_file_path):
  48.     # Video's duration in seconds, return a float number
  49.     _json = probe(vid_file_path)
  50.  
  51.     if 'format' in _json:
  52.         if 'duration' in _json['format']:
  53.             return float(_json['format']['duration'])
  54.  
  55.     if 'streams' in _json:
  56.         # commonly stream 0 is the video
  57.         for s in _json['streams']:
  58.             if 'duration' in s:
  59.                 return float(s['duration'])
  60.  
  61.     # if everything didn't happen,
  62.     # we got here because no single 'return' in the above happen.
  63.     raise NameError('I found no duration in file ' + vid_file_path)
  64.  
  65.  
  66. def seconds_to_d_h_m_s(temp_seconds):
  67.     i_sekunden = divmod(temp_seconds, 60)[1]
  68.     i_minuten1 = divmod(temp_seconds, 60)[0]
  69.     i_stunden1 = divmod(i_minuten1, 60)[0]
  70.     i_minuten2 = divmod(i_minuten1, 60)[1]
  71.     i_tage = divmod(i_stunden1, 24)[0]
  72.     i_stunden2 = divmod(i_stunden1, 24)[1]
  73.     if int(i_tage) > 0:
  74.         diff_time = str(int(i_tage)) + " Tage, " + str(int(i_stunden2)) + " Stunden, " + str(
  75.             int(i_minuten2)) + " Minuten, " + str(round(i_sekunden)) + " Sekunden"
  76.     elif int(i_stunden2) > 0:
  77.         diff_time = str(int(i_stunden2)) + " Stunden, " + str(int(i_minuten2)) + " Minuten, " + str(
  78.             round(i_sekunden)) + " Sekunden"
  79.     elif int(i_minuten2) > 0:
  80.         diff_time = str(int(i_minuten2)) + " Minuten, " + str(round(i_sekunden)) + " Sekunden"
  81.     else:
  82.         diff_time = str(round(i_sekunden)) + " Sekunden"
  83.  
  84.     return diff_time
  85.  
  86.  
  87. def has_video_endings(file):
  88.     media_endings = [".mp4", ".mkv", ".wmv", ".mov"]
  89.     return [file for ending in media_endings if file.lower().endswith(ending)]
  90.  
  91.  
  92. def has_link_endings(file):
  93.     link_endings = [".lnk", ".url"]
  94.     return [file for ending in link_endings if file.lower().endswith(ending)]
  95.  
  96.  
  97. def resolve_link(file):
  98.     if not has_link_endings(file):
  99.         return False
  100.     shell = win32com.client.Dispatch("WScript.Shell")
  101.     shortcut = shell.CreateShortCut(file)
  102.     real_file = shortcut.Targetpath
  103.     print("Link: " + file + " ------- > " + real_file)
  104.     return file_or_path(real_file)
  105.  
  106.  
  107. def file_counting(video_file):
  108.     global total_duration
  109.  
  110.     try:
  111.         file_duration = duration(video_file)
  112.     except NameError as err:
  113.         print(err)
  114.         return False
  115.     print(file_duration)
  116.  
  117.     total_duration += file_duration
  118.     zaehler_add(0)
  119.     return True
  120.  
  121.  
  122. def zaehler_add(mode):
  123.     global zaehler
  124.     global total_zaehler
  125.     if mode == 0:
  126.         zaehler += 1
  127.         total_zaehler += 1
  128.     if mode == 1:
  129.         total_zaehler += 1
  130.  
  131.  
  132. def progress_file(file):
  133.     if has_video_endings(file):
  134.         return file_counting(file)
  135.  
  136.     is_link = resolve_link(file)
  137.     if not is_link:
  138.         print("This file has no video ending and is not a link")
  139.         zaehler_add(1)
  140.         return False
  141.  
  142.  
  143. def progress_path(path):
  144.     # walk should not go down to symlinks
  145.     for (dirpath, dirnames, filenames) in os.walk(path):
  146.         if len(filenames) > 0:
  147.             # get_files[dirpath] = filenames
  148.             for filename in filenames:
  149.                 print(filename + " ------- > im Verzeichnis: " + dirpath)
  150.                 file_or_path(os.path.join(dirpath, filename))
  151.  
  152.     # for y in get_files:
  153.     #     for x in get_files[y]:
  154.     #         print(x + " ------- > im Verzeichnis: " + y)
  155.     #         file_or_path(os.path.join(y, x))
  156.     return True
  157.  
  158.  
  159. def file_or_path(x):
  160.     if type(x) != str or x is False:
  161.         return False
  162.  
  163.     if not os.path.exists(x):  # doppelt?
  164.         print("Das exististert nicht: " + x)
  165.         return False
  166.  
  167.     if os.path.isfile(x):
  168.         progress_file(x)
  169.     elif os.path.isdir(x):
  170.         progress_path(x)
  171.     else:
  172.         print("Das ist weder eine Datei, noch ein Pfad")
  173.         return False
  174.     return True
  175.  
  176.  
  177. if __name__ == "__main__":
  178.     if len(sys.argv) > 1:
  179.         files_paths = sys.argv[1:]
  180.     else:
  181.         files_paths = []
  182.         raw_input = input("Gib das Verzeichnis an: ")
  183.         files_paths.append(raw_input)
  184.         while True:
  185.             raw_input = input("Noch mehr Verzeichnisse? (y/n): ").strip()
  186.             if raw_input == "y":
  187.                 files_paths.append(input("Gib das zusätzliche Verzeichnis an: "))
  188.             else:
  189.                 break
  190.     print(files_paths)
  191.  
  192.     # get_files = {}
  193.     zaehler = 0
  194.     total_zaehler = 0
  195.     total_duration = 0
  196.  
  197.     for item in files_paths:
  198.         # get_files = {}
  199.         file_or_path(item)
  200.  
  201.     print(total_duration)
  202.     print(seconds_to_d_h_m_s(total_duration))
  203.     print(str(zaehler) + " / " + str(total_zaehler))
  204.     while input("Beenden?: y ") != "y":
  205.         time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement