Skidson
Twitter videos to mp4 - Printable Version

+- Skidson (https://skidson.online)
+-- Forum: Programming (https://skidson.online/Forum-Programming)
+--- Forum: Flex (https://skidson.online/Forum-Flex)
+--- Thread: Twitter videos to mp4 (/Thread-Twitter-videos-to-mp4)



Twitter videos to mp4 - eso - 05-02-2022

Insert a list of twitter video links in the "links" variable and specify where you want it to be dumped.

Code:
import requests
import json
import urllib.request
import os

path = r"path\to\directory"

links = """
twitter-video-link-1
twitter-video-link-2
"""

links = links.split("\n")
fails = []
for link in links:
    if link == "":
        continue
    twt_id = link.split("/")[5].split("?", 1)[0]
    if os.path.exists(r'{}\{}.mp4'.format(path, twt_id)):
        continue
    r = requests.get("https://tweetpik.com/api/tweets/{}/video".format(twt_id))
    data = json.loads(r.text)
    print("Downloading", twt_id)
    try:
        variants = data["variants"]
        bitrate = 0
        for variant in variants:
            if variant["bitrate"] > bitrate:
                bitrate = variant["bitrate"]
                dwld_link = variant["url"]

        urllib.request.urlretrieve(dwld_link, r'{}\{}.mp4'.format(path, twt_id))
    except:
        print("Failed to download", twt_id)
        fails += [link]

for fail in fails:
    print(fail)



RE: Twitter videos to mp4 - emma - 05-21-2022

I also suggest to use this Twitter to mp4 downloader.