Code Monkey home page Code Monkey logo

sub's Introduction

Subtitle Generation for Videos

This repository contains a Python code that automatically generates subtitles for videos. The code is designed to be run in Google Colaboratory or local.

alt text alt text

Prerequisites

Before running the code, make sure to install the necessary packages by executing the following commands:

on GoogleColab (Jupyter Notebook)

!pip install ffmpeg moviepy
!pip install faster-whisper
!apt install imagemagick
!cat /etc/ImageMagick-6/policy.xml | sed 's/none/read,write/g'> /etc/ImageMagick-6/policy.xml
!pip install --upgrade translators
!pip install -U kora

on local

pip install -r requirements.txt

Usage

To generate subtitles for a video, follow these steps:

Import the add_text_to_video function from the subpy_lib module:

from lib.subpy_lib import add_text_to_video

Define the following variables:

video_name: The link to the video. For example, "example/Steve Jobs Secrets of Life.mp4".

device_name: The computing device to be used. Set it to "cuda" if your GPU supports CUDA, otherwise set it to "cpu". Note that using CPU computation will be slower.

language_bool: Set it to False if you want to add subtitles in the original language. Set it to True if you want to translate the subtitles.

translation_language: The language to translate the subtitles to. Choose from the following options: ["bg", "cs", "da", "nl", "en", "et", "fi", "fr", "de", "el", "hu", "id", "it", "ja", "ko", "lv", "lt", "no", "pl", "pt", "ro", "ru", "sk", "sl", "es", "sv", "tr", "uk"].

Call the add_text_to_video function with the defined variables:

add_text_to_video(video_name, language_bool,translation_language,device_name)
  

Once the process is complete, a video player will be displayed showing the output video with subtitles.

Please note that the code uses the following steps to generate subtitles:

  • Extract the audio from the uploaded video and save it as result.mp3.
  • Perform speech recognition on the audio using the Faster Whisper model.
  • Create text clips for each detected segment of speech, with the corresponding start time, end time, and transcribed text.
  • If translation is enabled, translate the transcribed text to the specified language using the translators package.
  • Overlay the text clips onto the original video to create a new video with subtitles.
  • Save the output video as output.mp4 and display it using an HTML video player.

Feel free to modify the code and experiment with different parameters to customize the subtitle generation process.

Acknowledgements

This code utilizes the following packages:

ffmpeg and moviepy for video and audio processing

faster-whisper for speech recognition

translators for text translation

kora for file uploads to Google Colab

Demo

Original Video

Watch the video

Result

Watch the video

License

This code is released under the MIT License.

sub's People

Contributors

mrtnvr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.