|
from instagrapi import Client |
|
import requests |
|
from pathlib import Path |
|
import os |
|
from moviepy.editor import VideoFileClip |
|
from PIL import Image |
|
|
|
|
|
username = "droidcv1" |
|
password = ";ya_#GkM,KND6?$" |
|
|
|
|
|
cl = Client() |
|
|
|
|
|
cl.login(username, password) |
|
|
|
|
|
video_urls = [ |
|
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4", |
|
"https://samplelib.com/lib/preview/mp4/sample-10s.mp4", |
|
"https://samplelib.com/lib/preview/mp4/sample-15s.mp4" |
|
] |
|
|
|
|
|
def download_video(url, filename): |
|
response = requests.get(url, stream=True) |
|
if response.status_code == 200: |
|
with open(filename, 'wb') as f: |
|
for chunk in response.iter_content(1024): |
|
f.write(chunk) |
|
return filename |
|
|
|
|
|
def generate_thumbnail(video_path): |
|
clip = VideoFileClip(str(video_path)) |
|
frame = clip.get_frame(1) |
|
thumbnail_path = str(video_path) + ".jpg" |
|
im = Image.fromarray(frame) |
|
im.save(thumbnail_path) |
|
return thumbnail_path |
|
|
|
|
|
def upload_video(video_path, caption): |
|
thumbnail_path = generate_thumbnail(video_path) |
|
media = cl.clip_upload(video_path, caption=caption, thumbnail=thumbnail_path) |
|
return f"https://www.instagram.com/reel/{media.pk}/" |
|
|
|
|
|
download_dir = Path("downloads") |
|
download_dir.mkdir(exist_ok=True) |
|
|
|
|
|
for idx, video_url in enumerate(video_urls, start=1): |
|
video_path = download_video(video_url, download_dir / f"video_{idx}.mp4") |
|
caption = f"Sample video {idx}" |
|
reel_url = upload_video(video_path, caption) |
|
print(f"Uploaded video {idx} URL: {reel_url}") |
|
|
|
|
|
os.remove(video_path) |
|
os.remove(video_path.with_suffix('.mp4.jpg')) |
|
|