Can't download Full srt youtube captions

20 Views Asked by At

I'm trying to download YouTube subtitles from entire channel I used first Python code to extract all videos id and put them in text file

import os
from decouple import config
from googleapiclient.discovery import build

api_key = config('YOUTUBE_API_KEY')

if not api_key:
    raise ValueError("Please set the YOUTUBE_API_KEY environment variable.")

youtube = build('youtube', 'v3', developerKey=api_key)

def get_video_ids(channel_id):
    video_ids = []
    request = youtube.search().list(part='id', channelId=channel_id, maxResults=50, type='video')
    response = request.execute()

    while request is not None:
        try:
            for item in response['items']:
                video_id = item['id']['videoId']
                video_ids.append(video_id)
        except KeyError as ke:
            print(f"Error extracting video IDs: {ke}")
            break  # Stop the loop if there's an issue extracting video IDs
        request = youtube.search().list_next(request, response)
        response = request.execute() if request is not None else []

    return video_ids

def save_video_ids_to_file(video_ids, file_path='video_ids.txt'):
    with open(file_path, 'w') as file:
        for video_id in video_ids:
            file.write(video_id + '\n')

if __name__ == "__main__":
    channel_id = 'UCHnyfMqiRRG1u-2MsSQLbXA'
    video_ids = get_video_ids(channel_id)
    save_video_ids_to_file(video_ids)
    print(f"Number of videos in the channel: {len(video_ids)}")

And then I run second code to download .srt files. The problem is transcript not completed only few seconds.

import os
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import SRTFormatter

def download_subtitles_from_txt(input_file='video_ids.txt', output_folder='Youtubesubs'):
    # Create the output folder if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Read video IDs from the file
    with open(input_file, 'r') as file:
        video_ids = [line.strip() for line in file]

    try:
        for video_id in video_ids:
            try:
                # Get the English transcript for each video
                transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])

                # Create an instance of SRTFormatter
                srt_formatter = SRTFormatter()

                # Format the transcript in SRT
                srt_formatted = srt_formatter.format_transcript(transcript)

                # Save subtitles to a file (e.g., subtitles.srt) in the output folder
                file_path = os.path.join(output_folder, f'{video_id}_subtitles.srt')
                with open(file_path, 'w', encoding='utf-8') as file:
                    file.write(srt_formatted)

                print(f"Subtitles downloaded and saved for video {video_id}")

            except Exception as e:
                print(f"Error downloading subtitles for video {video_id}: {e}")

    except Exception as e:
        print(f"Error downloading subtitles: {e}")

if __name__ == "__main__":
    download_subtitles_from_txt()

When I try only one video it work fine but multi videos not, I used delay time for 10 seconds and nothing changed.

0

There are 0 best solutions below