Youtube transcript API not working on server

I have a Django web app. This code works perfectly fine on localhost but stops working when I run it on cloud (DigitalOcean) App Platform.

from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable

def transcribe(video_url):
    video_id = video_url.split("v=")[-1]
    logger.debug("Extracted video ID: %s", video_id)

    try:
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        transcript = None
        for transcript_info in transcript_list:
            try:
                transcript = transcript_info.fetch()
                break
            except Exception as e:
                logger.warning("Error fetching transcript: %s", e, exc_info=True)
                continue
        if transcript is None:
            logger.error("No transcripts available for this video.")
            return "No transcripts available for this video."
    except TranscriptsDisabled as e:
        logger.error("Transcripts are disabled for this video. %s", e, exc_info=True)
        return "Transcripts are disabled for this video."
    except NoTranscriptFound:
        logger.error("No transcript found for this video.")
        return "No transcript found for this video."
    except VideoUnavailable:
        logger.error("Video is unavailable.")
        return "Video is unavailable."
    except Exception as e:
        logger.error("Error in fetching transcript: %s", e, exc_info=True)
        return "Error in fetching transcript."
    
    # Concatenate all text from the transcript into a single string
    transcription_text = ' '.join([item['text'] for item in transcript])
    logger.debug("Transcription text (first 50 characters): %s", transcription_text[:50])

    return transcription_text

The part that throws an exception is the line transcript_list = YouTubeTranscriptApi.list_transcripts(video_id).

And it throws a TranscriptsDisabled exception, saying that

Transcripts are disabled for this video.

But I do know that the video has transcripts and the code works perfectly fine on localhost as mentioned.

After spending 2 days and trying literally anything I can think of, I still have no solution to this mysterious problem. Anyone who has experienced the same thing and managed to solve it in some way?

this error is because you don't use a proxy for your requests, your ip address could be blocked by youtube when you do your requests from a server. You can try to add proxies in your youtube-transcript-api configuration and let us know.

Back to Top