这是我在stackoverflow社区的第一篇帖子,我在闲暇时潜伏了几年,玩python。我已经写了一个脚本来修改Adobe Premiere Pro文件,以便将它们降级到“版本1”。这允许用户在旧版本的程序中打开新的项目文件。
现在,为了清楚,这之前已经做过了。在stackoverflow和Adobe论坛上有几个人发布了关于这个问题的问题和解决方案。我的问题与使用python gzip模块和带有lxml解析器的BeautifulSoup来解压缩和修改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纪录片,其中我的项目文件在压缩时超过100MB,解压时高达1.6 GB。我在一台内存为128 GB、处理器为3 GHz的Xeon上运行这个脚本。
我已经测试了GitHub上的其他几个脚本,它们在处理大型项目文件时表现出类似的行为。
我很乐意听到一些关于如何解决这个问题的想法。谢谢!
发布于 2020-01-15 18:02:51
如果我理解正确(如果我错了,请纠正我),您想要更改gzipped文件开头的一部分。
如果这是正确的,那么文件的未压缩版本是XML文件也没什么关系。唯一重要的是,您希望在压缩文件的开头稍作更改。
这总是很难实现的。压缩算法根据较早的信息存储较晚的信息,因此仅更改开头部分也会更改较晚的部分,或者换句话说:更改开头的一部分会导致必须同时更改压缩文件的其余部分,以便不更改文件的未压缩版本的其余部分。
在您的情况下,我担心您将不得不解压缩整个文件,更改它,然后再次压缩它。为了避免这种情况,您需要更改项目的体系结构,以便文件的开头和其余部分彼此分开存储,例如,存储在两个不同的文件中。然后,当需要XML信息时,使用软件需要将这两个文件缝合在一起。
如果您选择第一个选项并解压缩→change→重新压缩文件,那么您可以通过管道来完成此操作,这允许您在一个步骤中完成此操作。如果你需要更多的建议,请随时就这个话题提出另一个问题(然后从这里的评论链接到新的问题)。
发布于 2020-01-17 00:15:32
@Alfe,谢谢你的想法!通过使用正则表达式重写xml解析步骤,我自己解决了这个问题。在此之前,我没有深入研究过正则表达式,但我发现它的运行速度比Beautiful Soup解析器快了一个数量级。
我修改后的代码在github上:https://github.com/snorkem/prproj_downgrade/blob/master/prproj_downgrade.py
https://stackoverflow.com/questions/59671226
复制相似问题