yt-dlp is excellent for use as a library in your API! Here's how you can use it programmatically for Instagram video downloads:
Basic Usage Example
import yt_dlp
import json
def download_instagram_video(url, output_path='downloads/'):
ydl_opts = {
'outtmpl': f'{output_path}%(id)s.%(ext)s',
'quiet': True,
'no_warnings': True,
'extract_flat': False,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(url, download=True)
return {
'success': True,
'video_id': info.get('id'),
'title': info.get('title'),
'filename': ydl.prepare_filename(info),
'duration': info.get('duration'),
'uploader': info.get('uploader'),
}
except Exception as e:
return {'success': False, 'error': str(e)}
API Integration Example
from flask import Flask, request, jsonify
import yt_dlp
import os
app = Flask(__name__)
@app.route('/api/instagram/info', methods=['POST'])
def get_instagram_info():
"""Extract video info without downloading"""
url = request.json.get('url')
ydl_opts = {
'quiet': True,
'no_warnings': True,
'extract_flat': False,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
# Extract info without downloading
info = ydl.extract_info(url, download=False)
# Sanitize for JSON serialization
safe_info = ydl.sanitize_info(info)
return jsonify({
'success': True,
'data': {
'id': safe_info.get('id'),
'title': safe_info.get('title'),
'description': safe_info.get('description'),
'duration': safe_info.get('duration'),
'uploader': safe_info.get('uploader'),
'timestamp': safe_info.get('timestamp'),
'view_count': safe_info.get('view_count'),
'like_count': safe_info.get('like_count'),
'formats': [
{
'format_id': f.get('format_id'),
'ext': f.get('ext'),
'width': f.get('width'),
'height': f.get('height'),
'filesize': f.get('filesize'),
'url': f.get('url'),
} for f in safe_info.get('formats', [])
],
'thumbnail': safe_info.get('thumbnail'),
}
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 400
@app.route('/api/instagram/download', methods=['POST'])
def download_instagram():
"""Download video with specific options"""
url = request.json.get('url')
quality = request.json.get('quality', 'best')
# Configure download options
ydl_opts = {
'format': quality, # 'best', 'worst', or specific format
'outtmpl': 'downloads/%(id)s.%(ext)s',
'quiet': True,
'no_warnings': True,
'extract_flat': False,
# Add authentication if needed
# 'cookiefile': 'cookies.txt',
# Progress tracking
'progress_hooks': [lambda d: print(d['status'])],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
return jsonify({
'success': True,
'filename': os.path.basename(filename),
'path': filename,
'video_id': info.get('id'),
'title': info.get('title'),
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 400
Advanced Options for Instagram
# Instagram-specific options
ydl_opts = {
# Output template
'outtmpl': '%(uploader)s/%(title)s-%(id)s.%(ext)s',
# Format selection
'format': 'best[ext=mp4]/best',
# Authentication (if needed for private content)
'cookiefile': 'instagram_cookies.txt',
# Or use username/password
# 'username': 'your_username',
# 'password': 'your_password',
# Extract comments
'getcomments': True,
# Write metadata
'writeinfojson': True,
'writethumbnail': True,
# Subtitles/captions
'writesubtitles': True,
'writeautomaticsub': True,
# Post-processing
'postprocessors': [{
'key': 'FFmpegVideoConvertor',
'preferedformat': 'mp4',
}],
# Rate limiting
'ratelimit': 50000, # 50KB/s
'sleep_interval': 1, # Sleep 1 second between downloads
# Error handling
'ignoreerrors': True,
'continue': True,
}
Key Benefits:
- Full control over download options
- Progress tracking with hooks
- Error handling with exceptions
- Metadata extraction without downloading
- Format selection for quality/size optimization
- Authentication support for private content
- JSON-serializable output with sanitize_info()
Important Notes:
- Install with: pip install yt-dlp
- The library is thread-safe when creating new YoutubeDL instances
- Use extract_info(url, download=False) to get metadata only
- Always use sanitize_info() before JSON serialization
- Handle exceptions properly for production use
- Consider rate limiting to avoid IP blocks