Python-powered magic: Download Twitter videos with ease!

Twitter video downloader task.


Sometimies you need to download a video from some Twitter post and unfortunately you don't have such option available at least at the moment.

The problem can be solved with use of Twitter official APIs capabilities, but maybe you don't have enough time or desire for this.

Twitter video download with Python.
Twitter video download meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-11-20 by Andrey BRATUS, Senior Data Analyst.




We will solve this task by using youtube-dl Python library, don't forget to pip install it. You should specify the URL of the desired video and simply run the script below. The programm applicable for other cases when you know URL link of the video, i e not only for Twitter cases.

Twitter video downloader script.



# pip install youtube-dl

from __future__ import unicode_literals
import youtube_dl

link = 'https://twitter.com/i/status/1588160870743670784'

def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')

ydl_opts = {
    'format': 'bestaudio/best',       
    'outtmpl': '%(id)s.%(ext)s',        
    'noplaylist' : True,        
    'progress_hooks': [my_hook],  
    
}


with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([link])

print(f'Your video was downloaded.') 

OUT:
[twitter] 1588160870743670784: Downloading guest token
[twitter] 1588160870743670784: Downloading JSON metadata
[twitter] 1588160870743670784: Downloading m3u8 information
[download] 1588160870743670784.mp4 has already been downloaded
[download] 100% of 4.01MiB
Done downloading, now converting ...
Your video was downloaded.





See also related topics: