YouTube Live Capture

This module is used to capture images from YouTube live.

Näkymä Helsingistä location list

YouTube Capture Experiment

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info = ydl.extract_info(nakyma_helsinkigista_youtube_live_url, download=False)
    for o in info['formats']:
        if o['resolution'] == '1280x720':
            print(o['url'])
            break
    else:
        raise ValueError("No 1280x720 format")
[youtube] Extracting URL: https://www.youtube.com/watch?v=LMZQ7eFhm58
[youtube] LMZQ7eFhm58: Downloading webpage
[youtube] LMZQ7eFhm58: Downloading ios player API JSON
[youtube] LMZQ7eFhm58: Downloading web creator player API JSON
[youtube] LMZQ7eFhm58: Downloading m3u8 information
https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1730307113/ei/yQ8iZ5iHJdO-0u8P17eRmAs/ip/87.92.255.37/id/LMZQ7eFhm58.4/itag/232/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/sgovp/gir%3Dyes%3Bitag%3D136/rqh/1/hdlc/1/hls_chunk_host/rr5---sn-qo5-ixas.googlevideo.com/xpc/EgVo2aDSNQ%3D%3D/playlist_duration/3600/manifest_duration/3600/vprv/1/playlist_type/DVR/initcwndbps/1496250/met/1730285513,/mh/l8/mm/44/mn/sn-qo5-ixas/ms/lva/mv/m/mvi/5/pl/18/rms/lva,lva/dover/13/pacing/0/short_key/1/keepalive/yes/fexp/51312688,51326931/mt/1730285070/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,sgovp,rqh,hdlc,xpc,playlist_duration,manifest_duration,vprv,playlist_type/sig/AJfQdSswRAIgLr4jfvDw0XhywuQ1ZEwplZY3zZxKUQSAaokJHm-7VGwCICbQLin1Ce8JzYwHRA3VQNdmBS5xZJkSeEHg--IyR-sy/lsparams/hls_chunk_host,initcwndbps,met,mh,mm,mn,ms,mv,mvi,pl,rms/lsig/ACJ0pHgwRgIhAKS5fsddQ9nO5yB2IvHECSfqA_XByQtR8rn7t3otlqo2AiEA6T5TC-5Tfm8jif778GkDSetVkAzukM-ltx9mRRjLdVE%3D/playlist/index.m3u8

source

stream_url

 stream_url (ytlive_url:str, ydl_opts:dict)

source

show_frame

 show_frame (frame)

source

crop_frame

 crop_frame (frame, crop=(0, 0, 480, 30))

source

frame_to_text

 frame_to_text (frame)

source

known

 known (txt:str, known_places:str)

try to find one of known_places are included in the given txt

assert known("Torninnnnnn", nakyma_helsinki_known_places)=="Torni"

source

meta

 meta (frame, known_places=['Olympiaterminaali', 'Etelasatama',
       'Eteladsatama', 'Presidentinlinna', 'Tuomiokirkko', 'Kauppatori',
       'Kauppator', 'Torni', 'Valkosaari'], printing=False)

Withdraw meta data, datetime & place


source

fname

 fname (prefix, dt, pl)
url = stream_url(nakyma_helsinkigista_youtube_live_url, ydl_opts)
cap = cv2.VideoCapture(url)
ret, frame = cap.read()
if ret:
    show_frame(crop_frame(frame))
    print(frame_to_text(crop_frame(frame)))
    try:
        print(fname("cap_", *meta(crop_frame(frame), printing=True)))
    except Exception as e:
        print(e)
else:
    print("Failed to capture frame.")
[youtube] Extracting URL: https://www.youtube.com/watch?v=LMZQ7eFhm58
[youtube] LMZQ7eFhm58: Downloading webpage
[youtube] LMZQ7eFhm58: Downloading ios player API JSON
[youtube] LMZQ7eFhm58: Downloading web creator player API JSON
[youtube] LMZQ7eFhm58: Downloading m3u8 information

10.2024 1
10.2024 1
time data '10.2024 1' does not match format '%d.%m.%Y %H:%M:%S'

source

capture_youtube_live_frame

 capture_youtube_live_frame
                             (youtube_live_url:str='https://www.youtube.co
                             m/watch?v=LMZQ7eFhm58')

Capture a frame from the given YouTube Live URL and save into a JPEG file

file = capture_youtube_live_frame()
file
[youtube] Extracting URL: https://www.youtube.com/watch?v=LMZQ7eFhm58
[youtube] LMZQ7eFhm58: Downloading webpage
[youtube] LMZQ7eFhm58: Downloading ios player API JSON
[youtube] LMZQ7eFhm58: Downloading web creator player API JSON
[youtube] LMZQ7eFhm58: Downloading m3u8 information
30.10.2024 12:45:22 Tuomiokirkko |
Path('../data/cap_2024.10.30_12:45:22_Tuomiokirkko.jpg')
def crop_image(path, crop=(0, 0, 480, 30)): return Image.open(path).crop(crop)
def show_image(path):
    plt.imshow(crop_image(path))
    plt.axis('off')
    plt.show()

show_image(file)

Extract meta data from an image file

try:
    fname("cap_", *meta(files[1]))
except:
    pass

Rename files with meta info

“cap_2024.10.04_14:56:49_Presidentinlinna”

“cap_2024.10.06_19:04:14_Kauppatori”

“cap_2024.10.06_20:08:29_Kauppatori”

for i, o in enumerate(glob.glob("../data/f*.jpg")):
    try:
        new = Path("../data")/fname("cap_", *meta(o, True))
        print(i, o, " -> ", new)
        os.rename(o, new)
    except Exception as e:
        print(o)
../data/fail_2024.10.30_12:37:43_nowhere.jpg
../data/frame_1730210769.jpg
../data/frame_1730275818.jpg
../data/frame_1730216457.jpg
../data/frame_1730216703.jpg
../data/frame_1730210684.jpg
../data/frame_1730283579.jpg
../data/fail_2024.10.30_12:37:46_nowhere.jpg
../data/fail_2024.10.30_12:51:56_nowhere.jpg
../data/frame_1730210981.jpg
../data/fail_2024.10.30_12:37:49_nowhere.jpg
../data/fail_2024.10.30_12:38:06_nowhere.jpg
../data/frame_1730210632.jpg
../data/frame_1730210753.jpg
../data/frame_1730210641.jpg
../data/frame_1730275774.jpg
../data/frame_1730210660.jpg
../data/frame_1730211141.jpg
../data/fail_2024.10.30_12:37:38_nowhere.jpg
../data/fail_2024.10.30_12:38:01_nowhere.jpg
../data/frame_1730276077.jpg