update_format.py
批量转视频格式,超级慢,没什么卵用
import os
import asyncio
import subprocess
import concurrent.futures
import tracemalloc
tracemalloc.start()
# 创建日志文件
log_file = open('conversion_log.txt', 'w')
async def convert_mkv_to_mp4(input_file, output_file):
command = [r'D:\Python Software\ffmpeg-5.1.2-essentials_build\bin\ffmpeg.exe', '-i', input_file, '-codec:v', 'h264',
'-codec:a', 'aac', output_file]
process = await asyncio.create_subprocess_exec(*command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = await process.communicate()
# 将stdout和stderr写入日志文件
log_file.write(f'STDOUT:\n{stdout.decode()}\n')
log_file.write(f'STDERR:\n{stderr.decode()}\n')
# 等待子进程完成并获取输出,确保它完成
await asyncio.sleep(0) # 显式等待,可能帮助消除警告
return stdout, stderr
output_folder = rf'D:\tjjg_videos'
os.makedirs(output_folder, exist_ok=True)
source_folder = rf'E:\下载'
async def convert_files():
loop = asyncio.get_event_loop()
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
tasks = []
for root, dirs, files in os.walk(source_folder):
for file in files:
if file.endswith('.mkv'):
input_file_path = os.path.join(root, file)
output_file_path = os.path.join(output_folder, os.path.splitext(file)[0] + '.mp4')
# 正确传递参数给 convert_mkv_to_mp4
task = await loop.run_in_executor(executor, convert_mkv_to_mp4, input_file_path, output_file_path)
tasks.append(task)
# 等待所有子任务完成
await asyncio.gather(*tasks)
if __name__ == '__main__':
asyncio.run(convert_files())
update_mp4.py
只批量修改后缀名,但视频属性应该还是原mkv,不过适合我的业务场景
import os
import shutil
# 定义源文件夹和目标文件夹的路径
source_folder = r'E:\下载\预算员(土建与装饰)' # 替换为实际的源文件夹路径
target_folder = r'D:\tjjg_videos' # 替换为实际的目标文件夹路径
# 确保目标文件夹存在,如果不存在则创建
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# 遍历源文件夹中的所有文件和子文件夹
for root, _, files in os.walk(source_folder):
for filename in files:
# 检查文件是否是视频文件(可以根据需要添加其他文件扩展名)
if filename.lower().endswith(('.avi', '.mkv', '.mpg', '.mpeg', '.wmv', '.mov', '.flv', '.webm')):
source_file_path = os.path.join(root, filename)
# 创建新的文件名,将后缀改为.mp4
new_filename = '预算员(土建与装饰)' + os.path.splitext(filename)[0] + '.mp4'
target_file_path = os.path.join(target_folder, new_filename)
# # 移动文件并重命名
# shutil.move(source_file_path, target_file_path)
# 复制文件并重命名
shutil.copy(source_file_path, target_file_path)
print('拷贝并重命名完成')
upload_mp4.py
掉接口批量上传
import os
import requests
import time
# 目标URL
upload_url = "https://xxxxx.xxxxxx.com/xp/uploads/"
# 认证 token
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjk2Mzk5ODAwLCJpYXQiOjE2OTM4MDc4MDAsImp0aSI6IjJmZmZiNmE2N2Q3MzRjODg4YWUzZDI0YzEyYjQwMWMxIiwidXNlcl9pZCI6MywidXNlcm5hbWUiOiJcdTRlOGVcdTZkMGIifQ.0fa2HIGeDupbqD5f6Ed26Le7wD5dEjG-GCZm0-Ap-F8"
# 目录包含要上传的.mp4文件
source_directory = r"D:\tjjg_videos"
# 遍历目录,上传所有.mp4文件
for root, _, files in os.walk(source_directory):
for filename in files:
if filename.lower().endswith('.mp4'):
file_path = os.path.join(root, filename)
# 最多尝试上传三次
max_retries = 3
retries = 0
while retries < max_retries:
# 以二进制方式打开文件
with open(file_path, 'rb') as file:
# 准备请求参数
# 构建请求参数
payload = {
'file': (filename, open(file_path, 'rb')),
'resouce': (None, 'video'),
}
# 设置请求头,包括 Authorization
headers = {
'Authorization': f'{token}'
}
# 发送POST请求上传文件
response = requests.post(upload_url, files=payload, headers=headers)
# 检查响应状态码
if response.status_code == 200:
print(f"成功上传文件: {filename}")
print(response.text)
break
else:
print(f"上传文件失败: {filename}")
print(response.text)
retries += 1
if retries < max_retries:
print(f"重试上传文件: {filename} (第 {retries} 次)")
time.sleep(2) # 等待2秒后重试
else:
print(f"上传文件失败超过最大重试次数: {filename}")
print("上传完成")
del_mp4.py
批量删除垃圾数据
import asyncio
import aiohttp
# 目标URL
base_url = "https://xxxx.xxxxxx.com/xp/api/v1/resource/"
# 认证 token
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjk2Mzk5ODAwLCJpYXQiOjE2OTM4MDc4MDAsImp0aSI6IjJmZmZiNmE2N2Q3MzRjODg4YWUzZDI0YzEyYjQwMWMxIiwidXNlcl9pZCI6MywidXNlcm5hbWUiOiJcdTRlOGVcdTZkMGIifQ.0fa2HIGeDupbqD5f6Ed26Le7wD5dEjG-GCZm0-Ap-F8"
# 构建要删除的ID范围
start_id = 602
end_id = 2121
# 发送DELETE请求删除资源
async def delete_resource(session, resource_id):
url = f"{base_url}{resource_id}/"
headers = {
'Authorization': f'{token}'
}
async with session.delete(url, headers=headers) as response:
if response.status == 204:
print(f"成功删除资源: {url}")
else:
print(f"删除资源失败: {url}")
print(await response.text())
# 异步删除资源
async def delete_resources():
async with aiohttp.ClientSession() as session:
tasks = [delete_resource(session, resource_id) for resource_id in range(start_id, end_id + 1)]
await asyncio.gather(*tasks)
# 执行异步删除操作
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(delete_resources())
loop.close()
print("删除完成")