gpt4 book ai didi

python-3.x - 使用转换器将 .mp4 转换为 .mpeg4

转载 作者:行者123 更新时间:2023-12-04 22:54:16 25 4
gpt4 key购买 nike

我有一个 MP4 文件,我想将其转换为 MPEG4 文件。为此,我找到了 PythonVideoConvert 包。在 PyPI 页面上,给出了以下代码:

from converter import Converter
conv = Converter()

info = conv.probe('test/test1.avi')

PATH = 'C:/Users/.../'

convert = conv.convert(PATH +'Demo.mp4', PATH + 'Demo.mpeg4', {
'format': 'mpeg4',
'audio': {
'codec': 'aac',
'samplerate': 11025,
'channels': 2
},
'video': {
'codec': 'hevc',
'width': 720,
'height': 400,
'fps': 25
}})
当我运行这段代码时, convert对象被创建。但是,PATH 目录中没有 .mpeg4 视频。
因此,我有两个问题:
  • 上面的代码对于将 .mp4 文件转换为 .mpeg4 文件是否正确
  • 我需要运行什么来将转换后的视频保存为 .mpeg4 文件?

  • 根据 Selcuk 的评论,我运行了以下代码:
    for timecode in convert:
    pass
    这给出了错误:
    Traceback (most recent call last):

    File "<ipython-input-60-14c9225c3ac2>", line 1, in <module>
    for timecode in convert:

    File "C:\Users\20200016\Anaconda3\lib\site-packages\converter\__init__.py", line 229, in convert
    optlist = self.parse_options(options, twopass)

    File "C:\Users\20200016\Anaconda3\lib\site-packages\converter\__init__.py", line 60, in parse_options
    raise ConverterError(f'Requested unknown format: {str(f)}')

    ConverterError: Requested unknown format: mpeg4
    所以,我建议的格式似乎不正确。如何将视频转换为 .mpeg4?

    最佳答案

    我不认为PythonVideoConverter旨在在 Windows 中使用。
    我遇到了一个异常 AttributeError: module 'signal' has no attribute 'SIGVTALRM' , 因为 SIGVTALRM不是 Windows 中的有效信号。
    FFmpeg 和 FFprobe 命令行工具的默认路径,对于 Windows 也没有意义。
    在 Windows 中我们仍然可以使用该包,但建议设置 ffmpeg_pathffprobe_path .
    例子:

    conv = Converter(ffmpeg_path=r'c:\FFmpeg\bin\ffmpeg.exe', ffprobe_path=r'c:\FFmpeg\bin\ffprobe.exe')
    我们还必须通过设置 timeout=None 来禁用超时功能。争论。
    mpeg4不是有效的 FFmpeg 格式,但我们仍然可以将其用作文件扩展名。
    (格式为 FFmpeg 术语通常适用于容器格式)。
    当使用非标准文件扩展名时,我们必须设置 format入口。
    设置 'format': 'mp4'创建 MP4 文件容器(可以使用非标准 .mpeg4 文件扩展名创建)。

    完整的代码示例:
    from converter import Converter

    conv = Converter(ffmpeg_path=r'c:\FFmpeg\bin\ffmpeg.exe', ffprobe_path=r'c:\FFmpeg\bin\ffprobe.exe')

    #info = conv.probe('test/test1.avi')

    PATH = 'C:/Users/Rotem/'

    convert = conv.convert(PATH + 'Demo.mp4', PATH + 'Demo.mpeg4', {
    'format': 'mp4', #'format': 'mpeg4',
    'audio': {
    'codec': 'aac',
    'samplerate': 11025,
    'channels': 2
    },
    'video': {
    'codec': 'hevc',
    'width': 720,
    'height': 400,
    'fps': 25
    }},
    timeout=None)

    # https://pypi.org/project/PythonVideoConverter/
    for timecode in convert:
    print(f'\rConverting ({timecode:.2f}) ...')

    我们可以看到 Demo.mpeg4的媒体信息使用 MediaInfo工具:
    General
    Complete name : C:\Users\Rotem\Demo.mpeg4
    Format : MPEG-4
    Format profile : Base Media
    Codec ID : isom (isom/iso2/mp41)
    File size : 207 KiB
    Duration : 10 s 148 ms
    Overall bit rate mode : Variable
    Overall bit rate : 167 kb/s
    Writing application : Lavf58.45.100
    FileExtension_Invalid : braw mov mp4 m4v m4a m4b m4p m4r 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v

    Video
    ID : 1
    Format : HEVC
    Format/Info : High Efficiency Video Coding
    Format profile : Main@L3@Main
    Codec ID : hev1
    Codec ID/Info : High Efficiency Video Coding
    Duration : 10 s 0 ms
    Bit rate : 82.5 kb/s
    Width : 720 pixels
    Height : 400 pixels
    Display aspect ratio : 16:9
    Frame rate mode : Constant
    Frame rate : 25.000 FPS
    Color space : YUV
    Chroma subsampling : 4:2:0
    Bit depth : 8 bits
    Scan type : Progressive
    Bits/(Pixel*Frame) : 0.011
    Stream size : 101 KiB (49%)
    Writing library : x265 3.4+28-419182243:[Windows][GCC 9.3.0][64 bit] 8bit+10bit+12bit
    Encoding settings : ...
    Color range : Limited
    Codec configuration box : hvcC

    Audio
    ID : 2
    Format : AAC LC
    Format/Info : Advanced Audio Codec Low Complexity
    Codec ID : mp4a-40-2
    Duration : 10 s 148 ms
    Duration_LastFrame : -70 ms
    Bit rate mode : Variable
    Bit rate : 79.1 kb/s
    Maximum bit rate : 128 kb/s
    Channel(s) : 2 channels
    Channel layout : L R
    Sampling rate : 11.025 kHz
    Frame rate : 10.767 FPS (1024 SPF)
    Compression mode : Lossy
    Stream size : 98.0 KiB (47%)
    Title : IsoMedia File Produced by Google, 5-11-2011
    Language : English
    Default : Yes
    Alternate group : 1

    在 MediaInfo 输出中,MP4 文件容器应用“MPEG-4”格式...
    笔记:
    HEVC 视频格式应用 H.265 视频编解码器 - 在大多数情况下,编解码器被认为比容器更相关。

    关于python-3.x - 使用转换器将 .mp4 转换为 .mpeg4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70842018/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com