Skip to content

python3 script for downloading subtitles from youtube video #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions subtitle_downloader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Subtitle Downloader
```
This folder contains a python3 script to download subtitles from youtube videos, provided that they have subtitles.
```

# Requirements
1. youtube_transcript_api

```
pip install youtube_transcript_api
```
<p align='center'>or</p>

```
pip install -r requirements.txt
```

# How this script works
* Just execute the subtitle_downloader.py file with the youtube video link as a command line argument

```
python subtitle_downloader.py <video link>
```

* A new file named subtitle.txt is created in the same directory.

* This file contains the subtitles of the video given as argument along with timestamps.
1 change: 1 addition & 0 deletions subtitle_downloader/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
youtube_transcript_api==0.4.1
18 changes: 18 additions & 0 deletions subtitle_downloader/subtitle_downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Importing necessary modules
import sys
from youtube_transcript_api import YouTubeTranscriptApi

if len(sys.argv) != 2:
print("Enter valid number of arguments")
print("Try python subtitle_downloader.py <video link>")
sys.exit()

# The list of dictionaries obtained by the the get_transcript() function
# is stored in sub variable
url = sys.argv[1].split("?v=")[1]
sub = YouTubeTranscriptApi.get_transcript(url)

# Writing subtitles into a file.
with open("subtitles.txt", "w") as f:
for i in sub:
f.write("{}\n".format(i))