Merge branch 'master' into feature/ISSUE-23
This commit is contained in:
commit
3781907943
66
README.md
66
README.md
|
@ -1,9 +1,9 @@
|
||||||
|
|
||||||
# YouTube Transcript/Subtitle API (including automatically generated subtitles and subtitle translations)
|
# YouTube Transcript/Subtitle API (including automatically generated subtitles and subtitle translations)
|
||||||
|
|
||||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url) [](https://travis-ci.org/jdepoix/youtube-transcript-api) [](https://coveralls.io/github/jdepoix/youtube-transcript-api?branch=master) [](http://opensource.org/licenses/MIT) [](https://pypi.org/project/youtube-transcript-api/) [](https://pypi.org/project/youtube-transcript-api/)
|
|
||||||
|
|
||||||
This is an python API which allows you to get the transcripts/subtitles for a given YouTube video. It also works for automatically generated subtitles, supports translating subtitles and it does not require a headless browser, like other selenium based solutions do!
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url) [](https://travis-ci.com/jdepoix/youtube-transcript-api) [](https://coveralls.io/github/jdepoix/youtube-transcript-api?branch=master) [](http://opensource.org/licenses/MIT) [](https://pypi.org/project/youtube-transcript-api/) [](https://pypi.org/project/youtube-transcript-api/)
|
||||||
|
|
||||||
|
This is a python API which allows you to get the transcript/subtitles for a given YouTube video. It also works for automatically generated subtitles, supports translating subtitles and it does not require a headless browser, like other selenium based solutions do!
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
@ -149,13 +149,13 @@ for transcript in transcript_list:
|
||||||
|
|
||||||
# translating the transcript will return another transcript object
|
# translating the transcript will return another transcript object
|
||||||
print(transcript.translate('en').fetch())
|
print(transcript.translate('en').fetch())
|
||||||
|
|
||||||
# you can also directly filter for the language you are looking for, using the transcript list
|
# you can also directly filter for the language you are looking for, using the transcript list
|
||||||
transcript = transcript_list.find_transcript(['de', 'en'])
|
transcript = transcript_list.find_transcript(['de', 'en'])
|
||||||
|
|
||||||
# or just filter for manually created transcripts
|
# or just filter for manually created transcripts
|
||||||
transcript = transcript_list.find_manually_created_transcript(['de', 'en'])
|
transcript = transcript_list.find_manually_created_transcript(['de', 'en'])
|
||||||
|
|
||||||
# or automatically generated ones
|
# or automatically generated ones
|
||||||
transcript = transcript_list.find_generated_transcript(['de', 'en'])
|
transcript = transcript_list.find_generated_transcript(['de', 'en'])
|
||||||
```
|
```
|
||||||
|
@ -232,16 +232,16 @@ class MyCustomFormatter(Formatter):
|
||||||
return 'your processed output data as a string.'
|
return 'your processed output data as a string.'
|
||||||
```
|
```
|
||||||
|
|
||||||
## CLI
|
## CLI
|
||||||
|
|
||||||
Execute the CLI script using the video ids as parameters and the results will be printed out to the command line:
|
Execute the CLI script using the video ids as parameters and the results will be printed out to the command line:
|
||||||
|
|
||||||
```
|
```
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> ...
|
youtube_transcript_api <first_video_id> <second_video_id> ...
|
||||||
```
|
```
|
||||||
|
|
||||||
The CLI also gives you the option to provide a list of preferred languages:
|
The CLI also gives you the option to provide a list of preferred languages:
|
||||||
|
|
||||||
```
|
```
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en
|
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en
|
||||||
```
|
```
|
||||||
|
@ -252,9 +252,9 @@ You can also specify if you want to exclude automatically generated or manually
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --exclude-generated
|
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --exclude-generated
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --exclude-manually-created
|
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --exclude-manually-created
|
||||||
```
|
```
|
||||||
|
|
||||||
If you would prefer to write it into a file or pipe it into another application, you can also output the results as json using the following line:
|
If you would prefer to write it into a file or pipe it into another application, you can also output the results as json using the following line:
|
||||||
|
|
||||||
```
|
```
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --format json > transcripts.json
|
youtube_transcript_api <first_video_id> <second_video_id> ... --languages de en --format json > transcripts.json
|
||||||
```
|
```
|
||||||
|
@ -269,22 +269,28 @@ If you are not sure which languages are available for a given video you can call
|
||||||
|
|
||||||
```
|
```
|
||||||
youtube_transcript_api --list-transcripts <first_video_id>
|
youtube_transcript_api --list-transcripts <first_video_id>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If a video's ID starts with a hyphen you'll have to mask the hyphen using `\` to prevent the CLI from mistaking it for a argument name. For example to get the transcript for the video with the ID `-abc123` run:
|
||||||
|
|
||||||
|
```
|
||||||
|
youtube_transcript_api "\-abc123"
|
||||||
|
```
|
||||||
|
|
||||||
## Proxy
|
## Proxy
|
||||||
|
|
||||||
You can specify a https/http proxy, which will be used during the requests to YouTube:
|
You can specify a https/http proxy, which will be used during the requests to YouTube:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from youtube_transcript_api import YouTubeTranscriptApi
|
from youtube_transcript_api import YouTubeTranscriptApi
|
||||||
|
|
||||||
YouTubeTranscriptApi.get_transcript(video_id, proxies={"http": "http://user:pass@domain:port", "https": "https://user:pass@domain:port"})
|
YouTubeTranscriptApi.get_transcript(video_id, proxies={"http": "http://user:pass@domain:port", "https": "https://user:pass@domain:port"})
|
||||||
```
|
```
|
||||||
|
|
||||||
As the `proxies` dict is passed on to the `requests.get(...)` call, it follows the [format used by the requests library](http://docs.python-requests.org/en/master/user/advanced/#proxies).
|
As the `proxies` dict is passed on to the `requests.get(...)` call, it follows the [format used by the requests library](http://docs.python-requests.org/en/master/user/advanced/#proxies).
|
||||||
|
|
||||||
Using the CLI:
|
Using the CLI:
|
||||||
|
|
||||||
```
|
```
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> --http-proxy http://user:pass@domain:port --https-proxy https://user:pass@domain:port
|
youtube_transcript_api <first_video_id> <second_video_id> --http-proxy http://user:pass@domain:port --https-proxy https://user:pass@domain:port
|
||||||
```
|
```
|
||||||
|
@ -293,13 +299,13 @@ youtube_transcript_api <first_video_id> <second_video_id> --http-proxy http://us
|
||||||
|
|
||||||
Some videos are age restricted, so this module won't be able to access those videos without some sort of authentication. To do this, you will need to have access to the desired video in a browser. Then, you will need to download that pages cookies into a text file. You can use the Chrome extension [cookies.txt](https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg?hl=en) or the Firefox extension [cookies.txt](https://addons.mozilla.org/en-US/firefox/addon/cookies-txt/).
|
Some videos are age restricted, so this module won't be able to access those videos without some sort of authentication. To do this, you will need to have access to the desired video in a browser. Then, you will need to download that pages cookies into a text file. You can use the Chrome extension [cookies.txt](https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg?hl=en) or the Firefox extension [cookies.txt](https://addons.mozilla.org/en-US/firefox/addon/cookies-txt/).
|
||||||
|
|
||||||
Once you have that, you can use it with the module to access age-restricted videos' captions like so.
|
Once you have that, you can use it with the module to access age-restricted videos' captions like so.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from youtube_transcript_api import YouTubeTranscriptApi
|
from youtube_transcript_api import YouTubeTranscriptApi
|
||||||
|
|
||||||
YouTubeTranscriptApi.get_transcript(video_id, cookies='/path/to/your/cookies.txt')
|
YouTubeTranscriptApi.get_transcript(video_id, cookies='/path/to/your/cookies.txt')
|
||||||
|
|
||||||
YouTubeTranscriptApi.get_transcripts([video_id], cookies='/path/to/your/cookies.txt')
|
YouTubeTranscriptApi.get_transcripts([video_id], cookies='/path/to/your/cookies.txt')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -309,13 +315,13 @@ Using the CLI:
|
||||||
youtube_transcript_api <first_video_id> <second_video_id> --cookies /path/to/your/cookies.txt
|
youtube_transcript_api <first_video_id> <second_video_id> --cookies /path/to/your/cookies.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Warning
|
## Warning
|
||||||
|
|
||||||
This code uses an undocumented part of the YouTube API, which is called by the YouTube web-client. So there is no guarantee that it won't stop working tomorrow, if they change how things work. I will however do my best to make things working again as soon as possible if that happens. So if it stops working, let me know!
|
This code uses an undocumented part of the YouTube API, which is called by the YouTube web-client. So there is no guarantee that it won't stop working tomorrow, if they change how things work. I will however do my best to make things working again as soon as possible if that happens. So if it stops working, let me know!
|
||||||
|
|
||||||
## Donation
|
## Donation
|
||||||
|
|
||||||
If this project makes you happy by reducing your development time, you can make me happy by treating me to a cup of coffee :)
|
If this project makes you happy by reducing your development time, you can make me happy by treating me to a cup of coffee :)
|
||||||
|
|
||||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url)
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url)
|
||||||
|
|
|
@ -128,4 +128,8 @@ class YouTubeTranscriptCli(object):
|
||||||
help='The cookie file that will be used for authorization with youtube.'
|
help='The cookie file that will be used for authorization with youtube.'
|
||||||
)
|
)
|
||||||
|
|
||||||
return parser.parse_args(self._args)
|
return self._sanitize_video_ids(parser.parse_args(self._args))
|
||||||
|
|
||||||
|
def _sanitize_video_ids(self, args):
|
||||||
|
args.video_ids = [video_id.replace('\\', '') for video_id in args.video_ids]
|
||||||
|
return args
|
||||||
|
|
|
@ -81,6 +81,12 @@ class TestYouTubeTranscriptCli(TestCase):
|
||||||
self.assertEqual(parsed_args.format, 'pretty')
|
self.assertEqual(parsed_args.format, 'pretty')
|
||||||
self.assertEqual(parsed_args.languages, ['en'])
|
self.assertEqual(parsed_args.languages, ['en'])
|
||||||
|
|
||||||
|
def test_argument_parsing__video_ids_starting_with_dash(self):
|
||||||
|
parsed_args = YouTubeTranscriptCli('\-v1 \-\-v2 \--v3'.split())._parse_args()
|
||||||
|
self.assertEqual(parsed_args.video_ids, ['-v1', '--v2', '--v3'])
|
||||||
|
self.assertEqual(parsed_args.json, False)
|
||||||
|
self.assertEqual(parsed_args.languages, ['en'])
|
||||||
|
|
||||||
def test_argument_parsing__fail_without_video_ids(self):
|
def test_argument_parsing__fail_without_video_ids(self):
|
||||||
with self.assertRaises(SystemExit):
|
with self.assertRaises(SystemExit):
|
||||||
YouTubeTranscriptCli('--format json'.split())._parse_args()
|
YouTubeTranscriptCli('--format json'.split())._parse_args()
|
||||||
|
|
Loading…
Reference in New Issue