Last active
May 7, 2023 04:43
-
-
Save wangjiezhe/199ec3c9e3af4863933c549f22440b8c to your computer and use it in GitHub Desktop.
解压并用压缩文件名进行重命名/增加标题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import time | |
import mutagen | |
import win32api | |
from zipfile import ZipFile | |
from rich.progress import track | |
zip_regex = re.compile(r'.*\.zip$') | |
url_regex = re.compile(r'.*\.url$') | |
zip_list = [f for f in os.listdir() if zip_regex.match(f)] | |
failed_list = [] | |
for zip_file in track(zip_list): | |
zip_name = os.path.splitext(zip_file)[0] | |
with ZipFile(zip_file) as zf: | |
file_list = [f for f in zf.namelist() if not url_regex.match(f)] | |
for fi in file_list: | |
# 获取文件名 | |
file_name, file_ext = os.path.splitext(fi) | |
video_name = re.sub('%3f', '?', zip_name) | |
# 转码: avi -> mp4 | |
if file_ext in 'avi': | |
new_fi = file_name + '.mp4' | |
os.system("ffmpeg -i %s -c:v copy -c:a copy -y %s" % (fi, new_fi)) | |
fi = new_fi | |
# 转码: rm -> mp4 | |
if file_ext == 'rm': | |
new_fi = file_name + '.mp4' | |
os.system("ffmpeg -i %s -crf 18 -y %s" % (fi, new_fi)) | |
fi = new_fi | |
# 确定能够识别的格式: mp4, wmv | |
# mov 格式的文件需要先手动添加任意非空标题才能正常识别 | |
try: | |
tags = mutagen.File(fi, easy=True) | |
tags['Title'] = video_name | |
tags.save() | |
except: | |
failed_list.append(zip_file) | |
# 修正时间戳 | |
date_time = time.mktime(zf.getinfo(fi).date_time + (0,0,-1)) | |
os.utime(fi, (date_time, date_time)) | |
if len(failed_list) > 0: | |
res = win32api.MessageBox(None, '\n'.join(failed_list), 'Failed to rename files from:', 1) | |
if res == 1: | |
print('\n'.join(failed_list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import time | |
import mutagen | |
from zipfile import ZipFile | |
zip_regex = re.compile(r'.*\.zip$') | |
url_regex = re.compile(r'.*\.url$') | |
zip_list = [f for f in os.listdir() if zip_regex.match(f)] | |
for zip_file in zip_list: | |
zip_name = os.path.splitext(zip_file)[0] | |
with ZipFile(zip_file) as zf: | |
file_list = [f for f in zf.namelist() if not url_regex.match(f)] | |
# zf.extractall(members=file_list) | |
for fi in file_list: | |
zf.extract(fi) | |
# 修正时间戳 | |
date_time = time.mktime(zf.getinfo(fi).date_time + (0,0,-1)) | |
os.utime(fi, (date_time, date_time)) | |
# 获取文件名 | |
file_name, file_ext = os.path.splitext(fi) | |
video_name = re.sub('%3f', '?', zip_name) | |
# 转码: avi -> mp4 | |
if file_ext == 'avi': | |
new_fi = file_name + '.mp4' | |
os.system("ffmpeg -i %s -c:v copy -c:a copy -y %s" % (fi, new_fi)) | |
fi = new_fi | |
# 转码: rm -> mp4 | |
if file_ext == 'rm': | |
new_fi = file_name + '.mp4' | |
os.system("ffmpeg -i %s -crf 18 -y %s" % (fi, new_fi)) | |
fi = new_fi | |
# 确定能够识别的格式: mp4, wmv | |
tags = mutagen.File(fi, easy=True) | |
tags['Title'] = video_name | |
tags.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import time | |
import win32api | |
from zipfile import ZipFile | |
zip_regex = re.compile(r'.*\.zip$') | |
url_regex = re.compile(r'.*\.url$') | |
zip_list = [f for f in os.listdir() if zip_regex.match(f)] | |
failed_list = [] | |
for zip_file in zip_list: | |
zip_name = os.path.splitext(zip_file)[0] | |
with ZipFile(zip_file) as zf: | |
file_list = [f for f in zf.namelist() if not url_regex.match(f)] | |
for fi in file_list: | |
zf.extract(fi) | |
# 修正时间戳 | |
date_time = time.mktime(zf.getinfo(fi).date_time + (0,0,-1)) | |
os.utime(fi, (date_time, date_time)) | |
# 如果有多个文件,为了避免冲突,不改名字,留待手动解决 | |
if len(file_list) == 1: | |
file_ext = os.path.splitext(file_list[0])[1] | |
os.rename(file_list[0], zip_name + file_ext) | |
else: | |
failed_list.append(zip_file) | |
if len(failed_list) > 0: | |
res = win32api.MessageBox(None, '\n'.join(failed_list), 'Failed to rename files from:', 1) | |
if res == 1: | |
print('\n'.join(failed_list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import win32api | |
from zipfile import ZipFile | |
from rich.progress import track | |
zip_regex = re.compile(r'.*\.zip$') | |
url_regex = re.compile(r'.*\.url$') | |
zip_list = [f for f in os.listdir() if zip_regex.match(f)] | |
failed_list = [] | |
for zip_file in track(zip_list): | |
zip_name = os.path.splitext(zip_file)[0] | |
with ZipFile(zip_file) as zf: | |
file_list = [f for f in zf.namelist() if not url_regex.match(f)] | |
# 如果有多个文件,为了避免冲突,不改名字,留待手动解决 | |
if len(file_list) == 1: | |
file_ext = os.path.splitext(file_list[0])[1] | |
os.rename(file_list[0], zip_name + file_ext) | |
else: | |
failed_list.append(zip_file) | |
if len(failed_list) > 0: | |
res = win32api.MessageBox(None, '\n'.join(failed_list), 'Failed to rename files from:', 1) | |
if res == 1: | |
print('\n'.join(failed_list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from zipfile import ZipFile | |
from rich.progress import track | |
zip_regex = re.compile(r'.*\.zip$') | |
url_regex = re.compile(r'.*\.url$') | |
zip_list = [f for f in os.listdir() if zip_regex.match(f)] | |
failed_list = [] | |
for zip_file in track(zip_list): | |
zip_name = os.path.splitext(zip_file)[0] | |
with ZipFile(zip_file) as zf: | |
file_list = [f for f in zf.namelist() if not url_regex.match(f)] | |
for fi in file_list: | |
file_name, file_ext = os.path.splitext(fi) | |
os.rename(fi, file_name + ' ' + zip_name + file_ext) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment