twitter_video_download.py

import requests URL_PREFIX = 'https://video.twimg.com' def download_playlist(playlist_url): with requests.get(playlist_url) as request: return request.content.decode('utf-8').split("\n") def parse_playlist(playlist_content): chunk_list = [] for line in playlist_content: if line.startswith('#EXT-X-MAP:URI='): url = line[len('#EXT-X-MAP:URI="'):-1] chunk_list.append(url) elif line.startswith('#'): continue elif line: chunk_list.append(line) return chunk_list def download_and_merge_chunks(chunk_urls, output_file): open(output_file, 'w').close() chunk_index = 1 chunk_count = len(chunk_urls) for chunk_url in chunk_urls: url = f"{URL_PREFIX}{chunk_url}" print(f"Downloading chunk {chunk_index} of {chunk_count} from url {url}") request = requests.get(url) with open(output_file, 'ab') as f: f.write(request.content) chunk_index += 1 def ffmpeg_safe_copy(input_file, output_file): # Run ffmpeg safe copy # ffmpeg -i input_file -c:a copy -c:v copy -y output_file from os import remove from subprocess import Popen ffmpeg_cmd = [ 'ffmpeg', '-i', input_file, '-c:a', 'copy', '-c:v', 'copy', '-y', output_file ] with Popen(ffmpeg_cmd) as proc: proc.wait() print(f"Merged temp file into output file {output_file}") remove(input_file) def grab_playlist(playlist_url): playlist_content = download_playlist(playlist_url) chunk_list = parse_playlist(playlist_content) temp_file = 'temp.mp4' download_and_merge_chunks(chunk_list, temp_file) output_file = 'output.mp4' ffmpeg_safe_copy(temp_file, output_file) if __name__=='__main__': import argparse import pathlib parser = argparse.ArgumentParser(prog='playlist_grabber') parser.add_argument('url', help='url of playlist') args = parser.parse_args() grab_playlist(args.url)
Download video from Twitter in by specifying URL of a m3u8 playlist file

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.