├── requirements.txt ├── _config.yml ├── .gitignore ├── LICENSE ├── README.md ├── .github └── workflows │ └── python-publish.yml ├── setup.py └── ytthumb.py /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-modernist -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/__pycache__ 2 | **/venv 3 | **/tests 4 | **/thumbnails 5 | *.session 6 | *.session-journal 7 | *.pyc 8 | *.env 9 | *.jpg 10 | test.py 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Fayas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YouTube Video Thumbnail 2 | A simple youtube video thumbnail downloader with more qualities via youtube video link or id. 3 | 4 | ## Installation 5 | 6 | ``` 7 | pip install YTThumb 8 | ``` 9 | 10 | ## Usage 11 | 12 | ### Get Thumbnail 13 | 14 | ```py 15 | import ytthumb 16 | 17 | video = 'https://youtu.be/rokGy0huYEA' # link/id 18 | 19 | # Basic Usage 20 | print(ytthumb.thumbnail(video)) 21 | # => returns thumbnail link 22 | 23 | # Advanced Usage 24 | thumbnail = ytthumb.thumbnail( 25 | video=video, 26 | quality="sd" # Not required 27 | ) 28 | print(thumbnail) 29 | # returns thumbnail link 30 | ``` 31 | 32 | ### Get Qualities 33 | 34 | ```py 35 | import ytthumb 36 | 37 | print(ytthumb.qualities()) # json=True (default) 38 | # returns list of qualities with full form as json 39 | 40 | print(ytthumb.qualities(json=False)) 41 | # returns list of qualities as list 42 | ``` 43 | 44 | ### Download Thumbnail 45 | 46 | ```py 47 | import ytthumb 48 | 49 | ytthumb.download_thumbnail( 50 | video='https://youtu.be/rokGy0huYEA', 51 | name='thumbnail.jpg', # Not required 52 | quality='sd' # Not required 53 | ) 54 | # Downloaded thumbnail will be in the 'thumbnail.jpg' file in 'thumbnails' directory 55 | ``` 56 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Upload Python Package 10 | 11 | on: 12 | release: 13 | types: [published] 14 | 15 | permissions: 16 | contents: read 17 | 18 | jobs: 19 | deploy: 20 | 21 | runs-on: ubuntu-latest 22 | 23 | steps: 24 | - uses: actions/checkout@v3 25 | - name: Set up Python 26 | uses: actions/setup-python@v3 27 | with: 28 | python-version: '3.x' 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip 32 | pip install build 33 | - name: Build package 34 | run: python -m build 35 | - name: Publish package 36 | uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 37 | with: 38 | user: __token__ 39 | password: ${{ secrets.PYPI_API_TOKEN }} 40 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | import setuptools 3 | 4 | 5 | def requirements(file="requirements.txt"): 6 | if os.path.isfile(file): 7 | with open(file, encoding="utf-8") as r: 8 | return [i.strip() for i in r] 9 | else: 10 | return [] 11 | 12 | 13 | def readme(file="README.md"): 14 | if os.path.isfile(file): 15 | with open(file, encoding="utf-8") as r: 16 | return r.read() 17 | else: 18 | return "" 19 | 20 | 21 | setuptools.setup( 22 | name="YTThumb", 23 | version="1.4.7", 24 | description="YouTube video thumbnail downloader", 25 | long_description=readme(), 26 | long_description_content_type="text/markdown", 27 | license="MIT", 28 | author="Fayas", 29 | classifiers=[ 30 | "Intended Audience :: Developers", 31 | "Natural Language :: English", 32 | "Programming Language :: Python", 33 | "License :: OSI Approved :: MIT License", 34 | "Operating System :: OS Independent" 35 | ], 36 | project_urls={ 37 | "Tracker": "https://github.com/FayasNoushad/Youtube-Video-Thumbnail/issues", 38 | "Source": "https://github.com/FayasNoushad/Youtube-Video-Thumbnail" 39 | }, 40 | python_requires=">=3.6", 41 | py_modules=['ytthumb'], 42 | packages=setuptools.find_packages(), 43 | zip_safe=False, 44 | install_requires=requirements() 45 | ) 46 | -------------------------------------------------------------------------------- /ytthumb.py: -------------------------------------------------------------------------------- 1 | import os 2 | import requests 3 | 4 | 5 | def extract_id(video): 6 | 7 | # extract the id 8 | 9 | # if video is id 10 | if ("?" not in video) and ("/" not in video): 11 | return video 12 | 13 | # if video is a link 14 | if "/" in video: 15 | id = video.split("/")[-1] 16 | 17 | # There is a ?v= in "youtube.com" link format 18 | if "?v=" in video: 19 | id = id.split("?v=")[-1] 20 | 21 | # There is a "?feature=shared" in 22 | # "youtu.be" link format 23 | if "?" in id: 24 | id = id.split("?")[0] 25 | 26 | # returns extracted id 27 | return id 28 | 29 | 30 | def qualities(json=True): 31 | ''' 32 | json: 33 | True - for return qualities and full form as json (default) 34 | False - for return only qualities as list 35 | ''' 36 | json_qualities = { 37 | "sd": "Standard Quality", 38 | "mq": "Medium Quality", 39 | "hq": "High Quality", 40 | "maxres": "Maximum Resolution" 41 | } 42 | if not json: 43 | qualities = [] 44 | for i in json_qualities: 45 | qualities.append(i) 46 | else: 47 | qualities = json_qualities 48 | return qualities 49 | 50 | 51 | def thumbnail(video, quality='sd'): 52 | ''' 53 | video: 54 | id - video id 55 | link - video link 56 | quality: 57 | sd - Standard Quality 58 | mq - Medium Quality 59 | hq - High Quality 60 | maxres - Maximum Resolution 61 | ''' 62 | 63 | # extract the id 64 | video_id = extract_id(video) 65 | 66 | # check the qualities 67 | if quality not in qualities(): 68 | quality = 'sd' 69 | 70 | # thumbnail link 71 | thumbnail = f"https://img.youtube.com/vi/{video_id}/{quality}default.jpg" 72 | return thumbnail 73 | 74 | 75 | def download_thumbnail(video, name='thumbnail.jpg', quality='sd'): 76 | ''' 77 | video: 78 | id - video id 79 | link - video link 80 | name: 81 | thumbnail name 82 | quality: 83 | sd - Standard Quality 84 | mq - Medium Quality 85 | hq - High Quality 86 | maxres - Maximum Resolution 87 | ''' 88 | 89 | # Thumbnail link 90 | thumbnail_link = thumbnail(video, quality) 91 | 92 | # Download the thumbnail content 93 | thumbnail_data = requests.get(thumbnail_link).content 94 | 95 | # Save the thumbnail 96 | dir = "thumbnails" 97 | if not os.path.exists(dir): 98 | os.makedirs(dir) 99 | path = os.path.join(dir, name) 100 | with open(path, "wb") as file: 101 | file.write(thumbnail_data) 102 | 103 | # return path 104 | return path --------------------------------------------------------------------------------