Skip to content

Instantly share code, notes, and snippets.

@wewnumam
Last active August 11, 2026 15:50
Show Gist options
  • Select an option

  • Save wewnumam/457b84f2fbae158fe8d65ef4c2e6a8b1 to your computer and use it in GitHub Desktop.

Select an option

Save wewnumam/457b84f2fbae158fe8d65ef4c2e6a8b1 to your computer and use it in GitHub Desktop.
Frequently Used Codes

Python

Rename .mp4 video to md5 hash

import hashlib
import os

def get_md5(file_path):
    """Generates the MD5 hash of a file in chunks to save memory."""
    hash_md5 = hashlib.md5()
    with open(file_path, "rb") as f:
        # Read in 4KB chunks
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

def rename_to_md5(directory):
    # Supported extensions
    extensions = ('.jpg', '.jpeg', '.png', '.mp4')
    
    for filename in os.listdir(directory):
        if filename.lower().endswith(extensions):
            file_path = os.path.join(directory, filename)
            
            # Skip if it's a directory
            if os.path.isdir(file_path):
                continue
                
            file_ext = os.path.splitext(filename)[1]
            new_hash = get_md5(file_path)
            new_name = f"{new_hash}{file_ext}"
            new_path = os.path.join(directory, new_name)
            
            # Rename if the name isn't already the hash
            if filename != new_name:
                try:
                    os.rename(file_path, new_path)
                    print(f"Renamed: {filename} -> {new_name}")
                except FileExistsError:
                    print(f"Skipped: {new_name} already exists (Duplicate file).")

# Run the script in the current directory
if __name__ == "__main__":
    target_dir = "."  # Change this to your folder path
    rename_to_md5(target_dir)

Bash

youtube-dl download from list

#usr/bin/bash

for url in $(cat list.txt); do 
	youtube-dl $url -o './%(id)s.%(ext)s'
	echo $url
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment