Skip to content
Snippets Groups Projects
example03_slideshow.py 3.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • root's avatar
    root committed
    import pandas as pd
    from urllib.request import urlopen, ProxyHandler, build_opener, install_opener
    import json
    import os
    import wget
    
    # Dieses Beispiel erstellt eine Slideshow aus Bildern von Denkmälern in Neumünster, die URLs zu den Bildern sind in einer JSON Datei
    
    # These are proxy settings. If you are behind a proxy just comment them in and swap the ip and port with your proxy
    #proxy_support = ProxyHandler({"http": "http://<proxy-ip>:<proxy-port>",
    #                                "https": "http://<proxy-ip>:<proxy-port>"}) # maybe you need to use https instead of http in the address depending on your proxy settings
    #opener = build_opener(proxy_support)
    #install_opener(opener)
    
    # Die url zu der json datei
    url = "https://opendata.schleswig-holstein.de/dataset/eddb1d7e-7df3-421a-97c7-447e1b78c94c/resource/d413e41c-b13e-4984-8dbe-4725a9a188ec/download/denkmalliste.json"
    
    # download der json
    response = urlopen(url)
    
    # Lies die Json aus der Response aus
    data_json = json.loads(response.read())
    
    # Sammle die Fotos und die dazugehörigen Bezeichnungen aller Denkmäler in einer Liste
    FotoURL_list = []
    for denkmal in data_json:
    	if "FotoURL" in denkmal.keys():
    		FotoURL_list.append((denkmal["FotoURL"],denkmal["Bezeichnung"]))
    
    # Speichere "max" Anzahl an images in dem Ordner denkmal_imgs
    max = 10
    os.makedirs("denkmal_imgs", exist_ok=True)
    output_dir = "./denkmal_imgs/"
    file_list = []
    i = 0
    for img, bezeichnung in FotoURL_list[:max]:
    	out_path = output_dir + "img_" + str(i).zfill(3) + ".jpg"
    	if os.path.exists(out_path):
    		os.remove(out_path)
    	# Lade das Bild herunter und speichere es in dem Ordner denkmal_imgs
    	filename = wget.download(img, out=out_path)
    	# Schreibe die Bezeichnung auf das Bild mithilfe von ffmpeg
    	os.system(f"ffmpeg -y -i {filename} -vf \"drawtext=text='{bezeichnung}':fontcolor=white:box=1:boxcolor=black@0.6:fontsize=18:x=50:y=50:\" {filename}")
    	file_list.append(filename)
    	i = i + 1
    
    # Nutze ffmpeg zum erstellen einer Slideshow
    # Dauer pro angezeigtem Bild in Sekunden
    duration_image = 2.5
    # Dauer der Übergänge
    duration_transition = 0.5
    # Es wird ein langer Befehl erstellt um ffmpeg damit aufzurufen
    create_slideshow_command = "ffmpeg"
    for img in file_list:
    	create_slideshow_command += f" -loop 1 -t {duration_image} -i {img}"
    
    create_slideshow_command += " -filter_complex \""
    for i in range(len(file_list[:max])):
    	# Jedes Bild wird auf die gleiche Auflösung skaliert
    	create_slideshow_command += f"[{i}]scale=1028:764,pad=1028:764[a{i}];"
    
    # Die Übergänge werden hinzugefügt und eine slideshow daraus erstellt
    # -r 25 = framerate auf 25 fps, -pix_fmt yuv420p = pixel format
    # Die Art des Übergangs (Alternativen: slideright, fade, smoothleft, ...(https://trac.ffmpeg.org/wiki/Xfade)
    transition = "smoothleft"
    for i in range(len(file_list[:max])-1):
    	if i == 0:
            	create_slideshow_command += f" [a0][a1]xfade=transition={transition}:duration={duration_transition}:offset={(i+1) * (duration_image - duration_transition)}[fa{i}];"
    	elif i == len(file_list[:max])-2:
    		create_slideshow_command += f" [fa{i-1}][a{i+1}]xfade=transition={transition}:duration={duration_transition}:offset={(i+1) * (duration_image - duration_transition)}[fa{i}]\""
    	else:
    		create_slideshow_command += f" [fa{i-1}][a{i+1}]xfade=transition={transition}:duration={duration_transition}:offset={(i+1) * (duration_image - duration_transition)}[fa{i}];"
    
    create_slideshow_command += " -map \"[fa{}]\" -r 25 -vcodec libx264 slideshow.mp4".format(len(file_list[:max])-2)
    # Der Befehl wird an das System übergeben
    os.system(create_slideshow_command)