【问题标题】:Faster way to modify gzip xml file (Adobe Premiere Pro project file) in Python?在 Python 中修改 gzip xml 文件(Adobe Premiere Pro 项目文件)的更快方法?
【发布时间】:2020-01-09 19:54:20
【问题描述】:

这是我在业余时间潜伏和玩 python 几年后在 stackoverflow 社区的第一篇文章。我编写了一个脚本来修改 Adob​​e Premiere Pro 文件,以便将它们降级到“版本 1”。这允许用户在旧版本的程序中打开新的项目文件。

现在,要明确的是,这已经完成了。 stackoverflow 和 Adob​​e 论坛上有几个人发布了这个问题的问题和解决方案。我的问题与使用 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


    【解决方案1】:

    @Alfe,感谢您的想法!通过使用正则表达式重写 xml 解析步骤,我自己解决了这个问题。在此之前我没有深入研究过正则表达式,但我发现它的运行速度比 Beautiful Soup 解析器快一个数量级。

    我修改后的代码在 github 上: https://github.com/snorkem/prproj_downgrade/blob/master/prproj_downgrade.py

    【讨论】:

      【解决方案2】:

      如果我理解正确(如果我错了,请纠正我),您想更改压缩文件开头的一部分。

      如果这是正确的,那么文件的未压缩版本是 XML 文件并不重要。唯一重要的是您想在压缩文件的开头进行一些更改。

      这总是很难实现的。压缩算法会根据较早的信息存储后面的信息,因此仅更改开头也会更改后面的部分,或者反过来说:在开头更改部分需要还要更改压缩文件的其余部分,以免更改文件未压缩版本的其余部分。

      在您的情况下,我担心您将不得不解压缩整个文件,更改它,然后再次压缩它。为避免这种情况,您需要更改项目的体系结构,以便文件的开头和其余部分彼此分开存储,例如。 G。在两个不同的文件中。当需要 XML 信息时,使用软件需要将这两个文件拼接在一起。

      如果您选择第一个选项并解压缩→更改→重新压缩文件,那么您可以通过管道执行此操作,这样您就可以一步完成。如果您需要更多建议,请随时就该主题提出另一个问题(并从此处的评论链接到新问题)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多