【问题标题】:Python - getting duration of a video with ffprobe and splitting the video into piecesPython - 使用ffprobe获取视频的持续时间并将视频分成几部分
【发布时间】:2019-03-26 22:41:44
【问题描述】:

我正在尝试使用 ffmpeg 分割视频,实现 python 脚本。

我收到此错误:

Command '['ffprobe', '-i', 'path_to_my_video/MVI_0731.MP4', '-hide_banner']' returned non-zero exit status 1.

这是我用来分割视频的代码:

for video_file in video_files:
        try:
            # Call "ffprobe" to get duration of input video.
            ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, "-hide_banner"],
                                           check=True,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           encoding="utf-8",
                                           shell=True)

            # "ffprobe" writes to stderr instead of stdout!
            duration_string = extract_duration_from_ffprobe_output(ffprobe_process.stderr)            
            duration_in_seconds = duration_string_to_seconds(duration_string)

            # Make start_stop_list
            start_stop_list = read_start_stop_list(start_stop_lists[nbr_video])
            nbr_video += 1
            total_pieces = int(len(start_stop_list))

这是导致问题的行:

ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, "-hide_banner"],
                                           check=True,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           encoding="utf-8",
                                           shell=True)

当我把它改成这行代码时:

ffprobe_process = subprocess.run(args=['ffprobe', '-i', video_file, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])

它有效,我的意思是,脚本在该行之后,但随后在下一行引发以下错误:

470.240000
expected string or bytes-like object

470.240000 是我视频的正确时长。因此,我更改它的新行效果更好,但仍然无法以某种方式使用我的代码。

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 您将在 shell 中运行的命令是什么? cmd 标签适用于 Microsoft Windows cmd.exe。如果这不在 Windows 上,请删除 cmd 标签。

标签: python macos ffmpeg ffprobe


【解决方案1】:

我找到了解决方案。

这是有效的代码:

for video_file in video_files:
        try:
            # Call "ffprobe" to get duration of input video.
            ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])

            # "ffprobe" writes to stderr instead of stdout!
            #duration_string = extract_duration_from_ffprobe_output(ffprobe_process.stderr)     
            duration_string = str(ffprobe_process)   
            #duration_in_seconds = duration_string_to_seconds(duration_string)
            duration_in_seconds = duration_string

            # Make start_stop_list
            start_stop_list = read_start_stop_list(start_stop_lists[nbr_video])
            nbr_video += 1
            total_pieces = int(len(start_stop_list))
            #pdb.set_trace()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2015-11-24
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多