Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Deepseekv3
- Prompt:
- I'm downloading a movie that's packaged in multiple rar files. As soon as the first rar file is downloaded, I want to start unpacking it so I can start watching immediately. For an uninterrupted experience, the contents of the remaining rar files should be streamed into the file I'm already watching.
- The python script should run continuously to detect new rar files and stream their content into the existing file as they're downloaded
- Response:
- Here's a Python script that monitors a directory for RAR files, starts unpacking as soon as the first part is available, and continuously appends content from subsequent parts to allow uninterrupted playback:
- import os
- import time
- import patoolib
- import shutil
- from watchdog.observers import Observer
- from watchdog.events import FileSystemEventHandler
- class RarHandler(FileSystemEventHandler):
- def __init__(self, download_dir, output_file):
- self.download_dir = download_dir
- self.output_file = output_file
- self.processed_files = set()
- self.currently_processing = False
- self.initial_extract_done = False
- def on_created(self, event):
- if not event.is_directory and event.src_path.lower().endswith('.rar'):
- self.process_rar(event.src_path)
- def on_modified(self, event):
- if not event.is_directory and event.src_path.lower().endswith('.rar'):
- self.process_rar(event.src_path)
- def process_rar(self, rar_path):
- if rar_path in self.processed_files or self.currently_processing:
- return
- self.currently_processing = True
- try:
- print(f"Processing new RAR file: {rar_path}")
- # Extract to temporary directory first
- temp_dir = os.path.join(self.download_dir, "temp_extract")
- os.makedirs(temp_dir, exist_ok=True)
- # Extract the RAR file
- patoolib.extract_archive(rar_path, outdir=temp_dir)
- # Get extracted files (assuming single file extraction)
- extracted_files = [f for f in os.listdir(temp_dir)
- if not f.startswith('.') and os.path.isfile(os.path.join(temp_dir, f))]
- if not extracted_files:
- print(f"No files extracted from {rar_path}")
- return
- # For the first RAR file, we move the content to output
- if not self.initial_extract_done:
- for file in extracted_files:
- src = os.path.join(temp_dir, file)
- shutil.move(src, self.output_file)
- self.initial_extract_done = True
- print(f"Initial extraction complete. Output file created at {self.output_file}")
- else:
- # For subsequent RARs, we append the content
- for file in extracted_files:
- src = os.path.join(temp_dir, file)
- with open(src, 'rb') as fsrc, open(self.output_file, 'ab') as fdst:
- shutil.copyfileobj(fsrc, fdst)
- print(f"Appended content from {rar_path} to {self.output_file}")
- self.processed_files.add(rar_path)
- except Exception as e:
- print(f"Error processing {rar_path}: {str(e)}")
- finally:
- # Clean up temp directory
- if os.path.exists(temp_dir):
- shutil.rmtree(temp_dir)
- self.currently_processing = False
- def monitor_and_extract(download_dir, output_file):
- if not os.path.exists(download_dir):
- raise FileNotFoundError(f"Download directory {download_dir} does not exist")
- event_handler = RarHandler(download_dir, output_file)
- observer = Observer()
- observer.schedule(event_handler, download_dir, recursive=False)
- observer.start()
- print(f"Monitoring {download_dir} for RAR files. Output will be written to {output_file}")
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- observer.stop()
- observer.join()
- if __name__ == "__main__":
- import argparse
- parser = argparse.ArgumentParser(description='Continuous RAR file extractor and streamer')
- parser.add_argument('download_dir', help='Directory to monitor for RAR files')
- parser.add_argument('output_file', help='Path to the output file where content will be streamed')
- args = parser.parse_args()
- monitor_and_extract(args.download_dir, args.output_file)
- Requirements
- Install the required packages:
- pip install patool watchdog
- You'll also need unrar installed on your system:
- On Linux: sudo apt-get install unrar
- On macOS: brew install unrar
- On Windows: Download from RARLab
- How It Works
- The script monitors a directory for new or modified RAR files using watchdog.
- When the first RAR file appears, it extracts the content to your specified output file.
- As subsequent RAR files are downloaded, it extracts their content and appends it to the same output file.
- This allows media players to continue playing the file as new content is appended.
- Usage
- python rar_streamer.py /path/to/download/directory /path/to/output/file.mkv
- Notes
- This assumes all RAR files contain parts of a single continuous file (common for movie downloads).
- The script handles one RAR file at a time to avoid conflicts.
- Temporary extraction files are cleaned up after processing.
- For best results, use a media player that can handle growing files (like VLC).
- If the RAR files are password protected, you'll need to modify the script to handle that case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement