Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Created July 2, 2025 16:53
Show Gist options
  • Save ahmadrosid/ce3c4c74044dd80434ce12ffa3d7c9d4 to your computer and use it in GitHub Desktop.
Save ahmadrosid/ce3c4c74044dd80434ce12ffa3d7c9d4 to your computer and use it in GitHub Desktop.

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:

  1. Full control over download options
  2. Progress tracking with hooks
  3. Error handling with exceptions
  4. Metadata extraction without downloading
  5. Format selection for quality/size optimization
  6. Authentication support for private content
  7. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment