from videohash import VideoHash
import os

# Directory containing videos grouped by username
video_directory = '/path/to/videos'
hashes = {}

for username in os.listdir(video_directory):
    user_dir = os.path.join(video_directory, username)
    if os.path.isdir(user_dir):
        for video_file in os.listdir(user_dir):
            if video_file.endswith(('.mp4', '.mkv', '.avi')):  # Ensure it's a video file
                video_path = os.path.join(user_dir, video_file)
                try:
                    # Calculate the hash for each video
                    video_hash = VideoHash(path=video_path)
                    print(f"Hash for {video_file}: {video_hash.hash}")
                    
                    # Store hashes in a dictionary
                    if username in hashes:
                        hashes[username].append((video_file, video_hash.hash))
                    else:
                        hashes[username] = [(video_file, video_hash.hash)]
                except Exception as e:
                    print(f"Error processing {video_file}: {str(e)}")

def find_duplicates(hashes):
    duplicate_videos = []
    all_hashes = [(user, video, hsh) for user, videos in hashes.items() for video, hsh in videos]
    hash_dict = {}

    for user, video, hsh in all_hashes:
        if hsh in hash_dict:
            hash_dict[hsh].append((user, video))
        else:
            hash_dict[hsh] = [(user, video)]

    for videos in hash_dict.values():
        if len(videos) > 1:
            duplicate_videos.append(videos)

    return duplicate_videos

duplicates = find_duplicates(hashes)
for duplicate in duplicates:
    print("Duplicate videos found:")
    for video_info in duplicate:
        print(f"User: {video_info[0]}, Video: {video_info[1]}")
