Last active
June 23, 2024 09:04
-
-
Save alexisperrier/609a77b0cb8895437595d9dc2f754e29 to your computer and use it in GitHub Desktop.
Telecharge les videos TikTok a partir de leur URLs avec yt-dlp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Code généré en juin 2024 | |
| Telecharge les videos TikTok a partir de leur URLs avec yt-dlp | |
| Ce script télécharge efficacement plusieurs vidéos TikTok en parallèle tout en gérant les ressources et en implémentant une limitation de débit pour éviter de surcharger le serveur. | |
| ''' | |
| import json | |
| import pandas as pd | |
| import asyncio | |
| import yt_dlp | |
| import aiohttp | |
| class AsyncTikTokDL(yt_dlp.YoutubeDL): | |
| async def __aenter__(self): | |
| return self | |
| async def __aexit__(self, exc_type, exc, tb): | |
| await asyncio.sleep(0) # Dummy await | |
| async def extract_info_async(self, url): | |
| loop = asyncio.get_event_loop() | |
| return await loop.run_in_executor(None, self.extract_info, url) | |
| async def download_async(self, url): | |
| loop = asyncio.get_event_loop() | |
| return await loop.run_in_executor(None, self.download, [url]) | |
| async def download_tiktok(url, output_path, semaphore): | |
| ydl_opts = { | |
| 'format': 'best', | |
| 'outtmpl': f'{output_path}/%(id)s.%(ext)s', | |
| 'cookiesfrombrowser': ('chrome',), | |
| } | |
| async with semaphore: | |
| ydl = AsyncTikTokDL(ydl_opts) | |
| try: | |
| info = await ydl.extract_info_async(url) | |
| if info: | |
| print(f"Downloading: {info.get('title', 'Unknown Title')}") | |
| await ydl.download_async(url) | |
| print(f"Successfully downloaded: {url}") | |
| else: | |
| print(f"Failed to extract info for: {url}") | |
| except Exception as e: | |
| print(f"Error downloading {url}: {str(e)}") | |
| # Wait for 5 seconds between downloads | |
| await asyncio.sleep(5) | |
| async def download_multiple_tiktoks(urls, output_path, max_concurrent=3): | |
| semaphore = asyncio.Semaphore(max_concurrent) | |
| tasks = [download_tiktok(url, output_path, semaphore) for url in urls] | |
| await asyncio.gather(*tasks) | |
| async def main(username, urls): | |
| # definir le repertoire des vidéos | |
| output_path = f'./videos/{username}/' | |
| async with aiohttp.ClientSession() as session: | |
| await download_multiple_tiktoks(urls, output_path) | |
| if __name__ == "__main__": | |
| username = 'cnews' | |
| urls = ['https://www.tiktok.com/@cnews/video/7382980155112688928', | |
| 'https://www.tiktok.com/@cnews/video/7382981320491732256', | |
| ] | |
| asyncio.run(main(username, urls)) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Classe AsyncTikTokDL :
Fonction download_tiktok :
Fonction download_multiple_tiktoks :
Fonction main :
Exécution du script :
Ce script télécharge efficacement plusieurs vidéos TikTok en parallèle tout en gérant les ressources et en implémentant une limitation de débit pour éviter de surcharger le serveur.