如何使用python批量重命名具有质量信息的视频文件?

问题描述 投票:-1回答:1

我试图编写一个程序来重命名文件夹的所有视频文件。我只是想在当前文件名的末尾添加视频质量或尺寸,例如(720p)或(1080p)或类似的东西。但我收到以下错误:

Traceback (most recent call last):
  File "f:\Python Projects\Practice\mm.py", line 17, in <module>
    os.rename(file_name, f'{file_title} ({height}p){file_extension}')
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Video 1.mp4' -> 'Video 1 (1080p).mp4'

这是我的代码:

import os
from cv2 import cv2

os.chdir(r'F:\Python Projects\Practice\Temp Files')

for file_name in os.listdir():
    # Getting Video Resolution
    with open(file_name, 'r') as f:
        f_string = str(f).split('\'')[1]
        video_path = f'F:\\Python Projects\\Practice\\Temp Files\\{f_string}'
        video = cv2.VideoCapture(video_path)
        height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

    # Getting the title
    file_title, file_extension = os.path.splitext(file_name)
    os.rename(file_name, f'{file_title} ({height}p){file_extension}')

谁能告诉我如何解决此问题?在此先感谢...:)

python video-processing cv2 batch-rename
1个回答
0
投票

这样的事情。未测试。

import os
from cv2 import cv2

os.chdir(r'F:\Python Projects\Practice\Temp Files')

for file_name in os.listdir():
    # Getting Video Resolution
    f = open(file_name, 'r')
    f_string = str(f).split('\'')[1]
    video_path = f'F:\\Python Projects\\Practice\\Temp Files\\{f_string}'
    video = cv2.VideoCapture(video_path)
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    f.close()

    # Getting the title
    file_title, file_extension = os.path.splitext(file_name)
    os.rename(file_name, f'{file_title} ({height}p){file_extension}')
© www.soinside.com 2019 - 2024. All rights reserved.