【发布时间】:2020-01-09 19:54:20
【问题描述】:
这是我在业余时间潜伏和玩 python 几年后在 stackoverflow 社区的第一篇文章。我编写了一个脚本来修改 Adobe Premiere Pro 文件,以便将它们降级到“版本 1”。这允许用户在旧版本的程序中打开新的项目文件。
现在,要明确的是,这已经完成了。 stackoverflow 和 Adobe 论坛上有几个人发布了这个问题的问题和解决方案。我的问题与使用 python gzip 模块和 BeautifulSoup 与 lxml 解析器解压缩和修改 xml 文件的速度/效率有关。
代码如下:
# Assume I've done all the imports like gzip, bs4, pathlib, sys, etc.
#
def downgrade(prproj_in): # Main functionality of the program. Downgrades target prproj files.
"""
Shortened the docstring to save reading...
"""
new_version = '1'
root, ext = os.path.splitext(prproj_in) # Checking if file extension is correct.
new_name = (root + '_DOWNGRADED' + '(v.' + str(new_version) + ').prproj')
try:
if ext != '.prproj':
print('Invalid filetype. Must have valid .prproj extension.')
# If not a valid Adobe Premiere file, exit.
elif os.path.exists(new_name):
print('Output file already exists at this location. Please move or rename.')
else: # Otherwise... continue on to unzip and parse the xml file with BeautifulSoup.
with tqdm(total=100) as pbar: # Initialize progress bar.
with gzip.open(prproj_in, 'rt') as f: # Decompress project file and open...
file_content = f.read() # Put file contents into variable as string text
soup = BeautifulSoup(file_content, 'xml') # create soup object
print('Current project version: ' +
soup.Project.find_next()['Version']) # Printing current project version.
soup.Project.find_next()['Version'] = new_version # Change project version number to 1
print('Downgraded project version to: ' +
str(soup.Project.find_next()['Version'])) # Print new current version.
pbar.update(80)
with gzip.open(new_name, 'wt') as f_out:
f_out.write(str(soup)) # Turn soup object to string for final writing to gzip file.
pbar.update(100)
print('Downgrade Complete. New file: ' + new_name) # Change file extension.
except:
exception = sys.exc_info()
handle_exceptions(exception[0])
这里是解压后的.prproj文件的开头,相关属性我需要修改:
<?xml version="1.0" encoding="UTF-8" ?>
<PremiereData Version="3">
<Project ObjectRef="1"/>
<Project ObjectID="1" ClassID="62ad66dd-0dcd-42da-a660-6d8fbde94876" Version="30">
此代码在只有几 MB(解压缩前)的项目文件上运行良好,但在文件大小达到 60、70 或 80 MB 时运行最多需要 10 分钟。我目前正在制作一部 indy 纪录片,其中我的项目文件在压缩时超过 100 MB,在解压缩时高达 1.6 GB。我在配备 128 GB RAM 和 3 GHz Xeon 处理器的 iMac Pro 上运行此脚本。
我在 GitHub 上测试了一些其他脚本,它们在处理大型项目文件时似乎表现出类似的行为。
很想听听一些关于如何解决这个问题的想法。谢谢!
【问题讨论】:
标签: python-3.x xml beautifulsoup gzip lxml