Share Code python đặt tên video theo số thứ tự

shinohita

New member
Code python đặt tên video theo stt ( k biết giúp ích được gì cho mn không nhưng e dùng sắp xếp lại cho dễ làm việc )
1 nhập thư mục chứa video đầu vào
2 thư mục chứa video đầu ra , enter
nhớ cài python trên máy nhé các.
Python:
import os
import shutil

# Hàm để nhập đường dẫn từ người dùng
def input_directory(prompt):
    directory = input(prompt)
    while not os.path.exists(directory) or not os.path.isdir(directory):
        print("Thư mục không tồn tại. Hãy thử lại.")
        directory = input(prompt)
    return directory

# Nhập đường dẫn thư mục nguồn
source_directory = input_directory("Nhập đường dẫn thư mục nguồn: ")

# Nhập đường dẫn thư mục đích
destination_directory = input_directory("Nhập đường dẫn thư mục đích: ")

# Lấy danh sách tệp video trong thư mục nguồn
video_files = [file for file in os.listdir(source_directory) if file.endswith('.mp4')]

# Sắp xếp danh sách video theo thứ tự
video_files.sort()

# Đếm để gán số thứ tự
count = 1

# Duyệt qua từng tệp video và đổi tên, sau đó di chuyển đến thư mục đích
for video_file in video_files:
    new_name = f"{count}.mp4"
    source_path = os.path.join(source_directory, video_file)
    destination_path = os.path.join(destination_directory, new_name)
    shutil.copy(source_path, destination_path)  # Copy tệp vào thư mục đích
    count += 1

print(f"Đã đổi tên và di chuyển {count - 1} tệp video từ '{source_directory}' đến '{destination_directory}'.")

Để xem nội dung bị ẩn này, bạn cần viết bình luận. Nếu chưa có tài khoản, vui lòng Đăng ký hoặc Đăng nhập
 

Bài viết tương tự

Back
Top