Ifan24 / GPT_subtitles

Download YouTube video (or supply your own) and generate dual languange subtitles with OpenAI Whisper and translation API (GPT) 下载 YouTube 视频(或提供您自己的视频)并使用 Whisper 和翻译API (GPT) 生成双语字幕
MIT License
83 stars 16 forks source link
gpt subtitles translation

Dual Subtitles for Video

🎉 Welcome to try GPT Subtitler - a website for translating subtitles! [![GPT Subtitler Banner](assets/gpt_subtitler_en.png)](https://gptsubtitler.com) 🎉 [GPT Subtitler](https://gptsubtitler.com) is a web application inspired by this project, with many powerful features: ✨ Supports high-quality subtitle translation using various models such as Anthropic Claude, GPT-3.5, and GPT-4. Currently, the **Claude-Haiku** model is recommended. Additionally, the **Gemini-1.5-flash** and **Gemini-1.5-pro** models are available for free users to try, although they may not be as accurate as the Claude-Haiku model. 💰 New users receive 100,000 free tokens upon registration, enough to translate 20 minutes of video subtitles for free. 🎁 Free tokens can be claimed daily, and tokens can also be purchased at low prices. No API key is required to use AI translation. 👀 Real-time preview of translation results, support for editing prompts, few-shot examples, and the ability to stop translation at any time and restart from any position. After translation, multiple SRT subtitle file formats can be exported (translation + original or original + translation bilingual subtitles). 🙏 The website is currently in the early development stage and needs your support and feedback! You are welcome to try it out and provide valuable suggestions. 💬 If you encounter any bugs or have any suggestions during use, please feel free to raise an issue on the GitHub project or send feedback via email. Website link 👉 https://gptsubtitler.com/en Thanks for your support and for reading this far! 🙏 Here is a redeem code for 100,000 tokens: `GPTSubtitler_github_repo` You can use it at [Settings](https://gptsubtitler.com/settings)

Open In Colab

下载 YouTube 视频(或提供您自己的视频)并使用 Whisper 和翻译API生成双语字幕,中文文档请见 中文

This project is a Python script that downloads a YouTube video (or uses a local video file), transcribes it, translates the transcript into a target language, and generates a video with dual subtitles (original and translated). The transcription and translation are powered by the Whisper model and the translation API (M2M100, google, GPT3.5), respectively.

GPT-3.5 translation compared to Google Translate GPT-3.5 translation compared to Google Translate

Arguments: arguments

Requirements

Additionally, when running the script for first time, it will download the following pre-trained models:

Installation

  1. Clone this repository.
  2. Install the required dependencies using pip install -r requirements.txt

Usage

You can provide either a YouTube URL or a local video file for processing. The script will transcribe the video, translate the transcript, and generate dual subtitles in the form of an SRT file.

python main.py --youtube_url [YOUTUBE_URL] --target_language [TARGET_LANGUAGE] --model [WHISPER_MODEL] --translation_method [TRANSLATION_METHOD]

Arguments

Note: You must provide either --youtube_url or --local_video, but not both.

Example

To download a YouTube video, transcribe it, and generate subtitles in target language using the google api to translate:

python main.py --youtube_url [YOUTUBE_URL] --target_language 'zh' --model 'small' --translation_method 'google'

To process a local video file, transcribe it, and generate subtitles in target language using gpt3.5-16k (you will need to provide an OpenAI API key)):

python main.py --local_video [VIDEO_FILE_PATH] --target_language 'zh' --model 'medium' --translation_method 'gpt'

The script will generate the following output files in the same directory as the input video:

Subtitle Translation using GPT-3.5-16k (translate_gpt.py)

This script translates subtitles using OpenAI's GPT-3.5 language model. It requires an OpenAI API key to function. In most cases, GPT-based translation produce much better results compared to Google Translate, especially when dealing with context-specific translations or idiomatic expressions. This script aims to provide an alternative method for translating subtitles when traditional translation services like Google Translate do not produce satisfactory results.

Setup

  1. Sign up for an API key from OpenAI at https://platform.openai.com/account/api-keys
  2. Once you have the API key, create a file named .env in the same directory as the script.
  3. Add the following line to the .env file:
    OPENAI_API_KEY=your_api_key_here

    Replace your_api_key_here with the API key you obtained from OpenAI.

Usage

python translate_gpt.py --input_file INPUT_FILE_PATH [--batch_size BATCH_SIZE] [--target_language TARGET_LANGUAGE] [--source_language SOURCE_LANGUAGE] [--video_info VIDEO_INFO] [--model MODEL_NAME] [--no_mapping] [--load_tmp_file]

You can check the response.log file in the folder containing the input video file for live updates, similar to the experience with ChatGPT.

Arguments

Note:

Contributing

Contributions are more than welcome!

TODO

Google Colab Example

You can also try out this script using a Google Colab notebook. Click the link below to access the example:

Open In Colab

Follow the instructions in the notebook to download the necessary packages and models, and to run the script on your desired YouTube video or local video file.