#!/usr/bin/env python3 """ XHamster Video Downloader using yt-dlp This script downloads videos from xhamster2.com using the yt-dlp library. Please be aware of the legal and ethical considerations when downloading content. Usage: python xhamster_downloader.py Example: python xhamster_downloader.py "https://xhamster2.com/videos/step-sis-will-do-anything-to-make-me-delete-this-videos-xhGt5fJ" """ import sys import os import yt_dlp import logging import argparse # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('xhamster_downloader.log'), logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger(__name__) def download_xhamster_video(url, output_path=None, quality="720p"): """ Download a video from xhamster2.com using yt-dlp Args: url (str): The URL of the xhamster video to download output_path (str): Optional custom output path quality (str): Video quality (default: 720p) Returns: bool: True if successful, False otherwise """ # Format selector configuration format_selector = 'best' if quality != 'best': if quality.endswith('p'): height = quality[:-1] format_selector = f'bestvideo[height<={height}]+bestaudio/best[height<={height}]' else: format_selector = quality # Configure yt-dlp options ydl_opts = { 'format': format_selector, 'outtmpl': output_path or '%(title)s.%(ext)s', 'noplaylist': True, # Download only the video, not playlists 'nocheckcertificate': True, # Skip SSL certificate verification if needed 'verbose': False, # Set to True for detailed logging 'progress_hooks': [lambda d: logger.info(f"Progress: {d['status']} - {d.get('downloaded_bytes', 0)}/{d.get('total_bytes', 'unknown')} bytes")], 'postprocessors': [{ 'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4' }], } try: logger.info(f"Starting download for: {url}") with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=True) logger.info("Download completed successfully!") return True except yt_dlp.utils.DownloadError as e: logger.error(f"Download error: {e}") return False except Exception as e: logger.error(f"Unexpected error: {e}") return False def main(): """Main function to handle command line arguments and execute download""" parser = argparse.ArgumentParser(description="XHamster Video Downloader") parser.add_argument("url", help="URL of the video to download") parser.add_argument("-q", "--quality", default="720p", help="Video quality (e.g. best, 1080p, 720p, 480p) (default: 720p)") args = parser.parse_args() video_url = args.url # Validate URL if not video_url.startswith("http"): print("Error: Please provide a valid URL starting with http:// or https://") sys.exit(1) logger.info("XHamster Video Downloader Started") logger.info(f"Target URL: {video_url}") success = download_xhamster_video(video_url, quality=args.quality) if success: logger.info("Download process completed successfully!") print("Video downloaded successfully!") else: logger.error("Download process failed!") print("Failed to download video.") sys.exit(1) if __name__ == "__main__": main()