Skip to main content

blue tools by okwords.cn

Project description

Video2SRT Project (Video to Subtitle Project)

Project Overview

This is a simple project that converts speech from video into subtitles. FFmpeg Audio Extraction

FFmpeg Audio Extraction

The project uses FFmpeg to extract the audio stream from video files:

Input video formats: mp4, mkv, avi, flv, ts, m3u8, mov, wmv, asf, rmvb, vob, webm, and other formats

Output audio formats: wav, mp3, aac, flac, ogg, wma, m4a, aiff, and other formats

FFmpeg must be pre-installed and configured in the system environment variables.For detailed configuration methods, refer to the FFmpeg official website.

Whisper Speech Recognition

The project uses the Whisper model for speech recognition, supporting multilingual recognition (90+ languages).

Official Core Models: tiny: Tiny model, supports only core languages with low accuracy but fastest speed (approximately 108MB). base: Base model, supports mainstream languages with high accuracy and fast speed (approximately 1GB). This is the default model. small: Small model, supports multiple languages with high accuracy and medium speed (approximately 4GB). medium: Medium model, supports low-resource languages with high accuracy and slow speed (approximately 10GB). large: Large model, supports 99 languages + dialects (Cantonese, Wu Chinese, etc.) with high accuracy but slow speed (approximately 20GB).

Suitable for high-precision requirements and recognition of minority languages/dialects.Supported Languages List:[https://github.com/openai/whisper/blob/main/whisper/tokenizer.py]

Multilingual Translation

The project uses the facebook/m2m100 model and Google Translate API for multilingual translation, converting source language to target language (supports 100+ languages).

Optional Parameters: is_translate: Default value: False Optional values: True

translate_engine: Default value: model: facebook/m2m100 local model (private, free and open-source; the model needs to be downloaded on first use, with slow speed) Optional values: api: Google Translate API (free, with fast speed)

Supported Languages List: facebook/m2m100 model: [https://huggingface.co/facebook/m2m100_418M/blob/main/README.md] Google Translate API: [https://cloud.google.com/translate/docs/languages]

GPU Acceleration

To use GPU acceleration for steps 2 and 3, CUDA must be installed along with the GPU-supported version of torch compatible with the installed CUDA version.For detailed installation methods, refer to the PyTorch official website.

Video2SRT Usage Steps

  1. Install environment software: Ensure Python 3.13.5 or higher is installed, and FFmpeg is installed and configured in the system environment variables.

  2. Install project dependencies (Project dependency file: [pyproject.toml]; project dependencies can be installed using pip or uv)

  3. Run the project code (Project main file: [video2srt.py])

Install Environment Software

Ensure Python 3.13.5 or higher is installed [https://www.python.org/downloads/] Ensure FFmpeg is installed and configured in the system environment variables [https://ffmpeg.org/download.html]

### Install FFmpeg
# CentOS
yum install ffmpeg ffmpeg-devel -y

# Ubuntu/MacOS
apt install ffmpeg
brew install ffmpeg

# Windows: Download the FFmpeg installation package, place it in a specific directory, and add the path to the system Path.
# Download URL: https://ffmpeg.org/download.html

# Verify installation
ffmpeg -version # Check FFmpeg version
ffmpeg -formats # View all formats supported by FFmpeg

Install Project Dependencies

Python >= 3.13.5

Using uv (Recommended)

Project configuration file: [project.toml]

uv python install 3.14
uv python pin 3.14
uv sync
uv lock

Using Pip

Install required packages (Project dependency file: [requirements.txt])

pip install deep_translator
pip install openai-whisper
pip install transformers torch
pip install sentencepiece

# To speed up installation, users in mainland China can add -i to each command to use domestic pip mirrors. For example:
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

# You can also install directly using the requirements.txt file
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

Run Project Code

The Whisper model (the base model is used by default) needs to be downloaded on the first run. The model is approximately 1GB in size and will take some time. Please wait patiently.

Usage Example (sample)

    from video2srt import video_to_srt
    srt_lines = video_to_srt(
        video_path = "test_video.mp4", 
        srt_output_path = "test_video.mp4",
        language="zh"
    )
    
    print("Sample SRT 0-7 lines:")
    print("\n".join(srt_lines[:7]))

Usage Example (Full Parameter Configuration)

# ## Import the video_to_srt function
from video2srt import video_to_srt

# ## Parameters Configuration

VIDEO_PATH = input("Please input video file path:")  
# Video file path
SRT_OUTPUT = "test_out.srt"  
# Output SRT path
MODEL_SIZE = "base"  
# Whisper model type (tiny/base/small/medium/large)
# Defaults to base; large offers higher accuracy but requires more VRAM and processing time, and supports minority languages and dialects
LANGUAGE = None       
# Recognition language, auto-detected by default, supports multilingual recognition (90+ languages) (e.g., en/zh/ja/lo/fr/de, etc.; refer to Whisper documentation for more languages)
IS_TRANSLATE = False  
# Whether to translate recognized text, disabled by default
TRANSLATE_ENGINE = "model"  
# Translation engine (model/api); model uses the local facebook/m2m100_418M model by default, api uses Google Translate API by default
TRANSLATE_LANG = "zh"      
# Target translation language (e.g., zh/en/ja/ko/fr), defaults to Chinese, supports translation to 100+ languages. Basically consistent with Whisper but with some differences (e.g., no dialect support: zh/yue/wuu -> zh), the system will automatically convert them to zh
USE_GPU = True      
# Whether to use GPU acceleration, uses GPU by default, if no GPU is available, uses CPU

# ## Generate SRT (using the video_to_srt function)
srt_lines = video_to_srt(
    video_path = VIDEO_PATH,
    srt_output_path = SRT_OUTPUT,
    model_size = MODEL_SIZE,
    language = LANGUAGE,
    is_translate = IS_TRANSLATE,
    translate_engine = TRANSLATE_ENGINE,
    translate_lang = TRANSLATE_LANG,
    use_gpu = USE_GPU
)

print("Sample SRT 0-7 lines:")
print("\n".join(srt_lines[:7]))

View Help and Samples

from video2srt import sample, hello_video2srt

hello_video2srt()

srt_lines = sample()
print("Sample SRT 0-7 lines:")
print("\n".join(srt_lines[:7]))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

video2srt-0.1.7.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

video2srt-0.1.7-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file video2srt-0.1.7.tar.gz.

File metadata

  • Download URL: video2srt-0.1.7.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for video2srt-0.1.7.tar.gz
Algorithm Hash digest
SHA256 863640a73d28e3180ef7285627a16f4a591434be26308d7338d3ebcad1848984
MD5 ca3362be50275f18b35baba114069262
BLAKE2b-256 677c5dfbe99faa290318a116e1a3515b958f5b362237b0862f74bdb070f9ffc1

See more details on using hashes here.

File details

Details for the file video2srt-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: video2srt-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for video2srt-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 48e107b57c5f3343b2183f443a05947a515d5ba7d083f36f3d7ec2b15ebdd2f5
MD5 1e1bae7748915737c88c50d3375433fc
BLAKE2b-256 0ee43893c1c1f0ce16b22c9ddc8ada2ee31aa6d646e58f6b5d4729603fbfceab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page