Sample codes: how to retrieve some data from Youtube channels¶

In [89]:
from youtube_transcript_api import YouTubeTranscriptApi
import googleapiclient.discovery
import requests
import os

api_service_name = "youtube"
api_version      = "v3"
YOUTUBE_API_KEY  = "------------------"
CHANNEL_ID       = "UC9AE6MneoakfIEOcWc8hUUg"

youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = YOUTUBE_API_KEY)
In [90]:
###################################
# return video_ids given channel id
###################################
def get_video_ids(channel_id):
    request = youtube.search().list(
        part='id',
        channelId=channel_id,
        maxResults=4,        # retireve maximum 4 videos from the channel
        type='video'
    )
    response = request.execute()
    video_ids = []
    for item in response['items']:
        video_ids.append(item['id']['videoId'])
    return video_ids
In [91]:
############################################
# Download the thumbnail of the given video
# and save it into the directory
############################################
def download_thumbnail(video_id, save_folder):
    request = youtube.videos().list(
        part='snippet',
        id=video_id
    )
    response = request.execute()

    thumbnail_url = response['items'][0]['snippet']['thumbnails']['high']['url']
    response = requests.get(thumbnail_url)
    if response.status_code == 200:
        with open(os.path.join(save_folder, f'{video_id}.jpg'), 'wb') as f:
            f.write(response.content)
    else:
        print(f"Failed to download thumbnail for video ID: {video_id}")
In [92]:
##############################################################
# Statistics of the video, e.g., views, likes, or comments...
##############################################################
def get_statistics(video_ids):

    # Fetch video statistics
    stats_request = youtube.videos().list(
        part='statistics',
        id=','.join(video_ids)
    )
    stats_response = stats_request.execute()
    
    videos = []
    for item in stats_response['items']:
        if int(item['statistics']['viewCount']) > 10000:
            videos.append({
                'videoId': item['id'],
                'viewCount': item['statistics']['viewCount'],
                'likeCount': item['statistics']['likeCount'],
                'commentCount': item['statistics']['commentCount'],
            })
    return videos
In [93]:
# Testing the get_video_ids function 
video_ids        = get_video_ids(CHANNEL_ID)
print(video_ids)
['Bzu-fbq1LNQ', 'oh-Xy0AyMPA', 'Q7Aw1ihRNSo', 'bBcAW7qMQ8c']
In [94]:
# Testing the get_statistics function
video_statistics = get_statistics(video_ids)
print(video_statistics)
[{'videoId': 'Bzu-fbq1LNQ', 'viewCount': '231816', 'likeCount': '4416', 'commentCount': '58'}, {'videoId': 'oh-Xy0AyMPA', 'viewCount': '135788', 'likeCount': '1650', 'commentCount': '34'}, {'videoId': 'Q7Aw1ihRNSo', 'viewCount': '217578', 'likeCount': '2233', 'commentCount': '87'}, {'videoId': 'bBcAW7qMQ8c', 'viewCount': '255003', 'likeCount': '8679', 'commentCount': '146'}]

Sample code: how to extract some data from thumbnail¶

In [95]:
import cv2
import easyocr
import numpy as np
import matplotlib.pyplot as plt
In [96]:
def face_recognition(image_file):

    # Haar Cascade
    cascade_path = save_folder + "\\haarcascade_frontalface_default.xml"

    # Haar Cascade 
    face_cascade = cv2.CascadeClassifier(cascade_path)

    # Read image
    image = cv2.imread(image_file)
    gray  = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect face
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    # Draw a rectangle in the detected face
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)

    # Show the result
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
In [97]:
def text_recognition(image_file, lang):

    # Read image
    image = cv2.imread(image_file)
    gray  = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # reduce the image size
    new_width = 400
    scale_ratio = new_width / image.shape[1]
    new_height = int(image.shape[0] * scale_ratio)
    image = cv2.resize(image, (new_width, new_height))

    reader = easyocr.Reader([lang])
    results = reader.readtext(image)
    for (bbox, text, prob) in results:
        (top_left, top_right, bottom_right, bottom_left) = bbox
        top_left = (int(top_left[0]), int(top_left[1]))
        bottom_right = (int(bottom_right[0]), int(bottom_right[1]))

        print(f"Detected Text: {text} (Confidence: {prob:.2f})")
        cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)


    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
In [98]:
save_folder = "./"
VIDEO_ID = "o97upTCsRME"
download_thumbnail(VIDEO_ID, save_folder)
face_recognition(save_folder + VIDEO_ID + '.jpg')
In [99]:
VIDEO_ID = "o97upTCsRME"
text_recognition(save_folder + VIDEO_ID + '.jpg', "ja")
VIDEO_ID = "1i9kcBHX2Nw" #"o97upTCsRME"
text_recognition(save_folder + VIDEO_ID + '.jpg', "en")
Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.
Detected Text: 伝説のスピー手 (Confidence: 0.73)
Detected Text: スティーブ (Confidence: 0.48)
Detected Text: ジョブズ (Confidence: 0.44)
Detected Text: スタンフォコザ演観 (Confidence: 0.02)
Detected Text: 英語・日本語字幕 (Confidence: 0.79)
Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.
Detected Text: ENG (Confidence: 0.42)
Detected Text: SH (Confidence: 0.78)
Detected Text: 'VIEWS (Confidence: 0.42)
Detected Text: SPL (Confidence: 0.84)
Detected Text: STEVE JOBS (Confidence: 0.70)
Detected Text: SPEECH IN ENGLISH WITH SUBTITLES (Confidence: 0.70)
In [ ]: