【问题标题】:Crop MP3 to last N seconds将 MP3 裁剪为持续 N 秒
【发布时间】:2015-08-29 23:26:59
【问题描述】:

我想从现有 MP3 文件生成一个新的(完全有效的)MP3 文件。新文件应该只包含曲目的最后 N 秒。

avconv 中有 -t 选项用于获取前 N 秒:

-t 持续时间(输出)

在其持续时间达到持续时间后停止写入输出。 duration 可以是以秒为单位的数字,也可以是“hh:mm:ss[.xxx]”形式。

是否有裁剪最后 N 秒的选项? avconv 在我的情况下不是必需的,所有其他工具都可以接受。

【问题讨论】:

    标签: ffmpeg mp3 avconv


    【解决方案1】:

    您可以简单地使用ffprobe 来获取音频的持续时间。

    ffprobe -i input_audio -show_entries format=duration -v quiet -of csv="p=0"
    

    计算nth_second并将其与ffmpeg一起使用

    ffmpeg -i input_audio -ss nth_second output_file
    

    以下是该进程的 shell 脚本。

    #!/bin/bash
    duration=$(ffprobe -i ${1} -show_entries format=duration -v quiet -of csv="p=0")
    nth_second=$(echo "${duration} - ${2}"|bc)
    ffmpeg -i ${1} -ss ${nth_second} ${3}
    

    【讨论】:

      【解决方案2】:

      如果您想发布脚本,您应该检查以确保安装了 mp3info,以及参数计数等,但这应该可以帮助您开始。将其保存为 .sh 并使用 mp3lastnsecs.sh input.mp3 output.mp3 numsecs 调用它

      此外,您还需要添加其他 ffmpeg 标志以用于标记、比特率等。

      #!/usr/bin/bash
      
      SECS=$(mp3info -p "%S" $1)
      if [ "$SECS" -ge "$3" ]; then
        START=$((SECS-$3))
      else
        echo 'Error: requested length is longer than the song.'
        exit 1
      fi
      
      #echo Starting song $1 from $START seconds.
      ffmpeg -i "$1" -ss $START "$2"
      

      样本输出:

      [ ? @ ? tmp]? ./lastN.sh 01-glaube.mp3 output.mp3 20
      Starting song 01-glaube.mp3 from 96 seconds.
      ffmpeg -i 01-glaube.mp3 -ss 96 output.mp3 # I had an echo in there for testing, it now calls ffmpeg directly
      [ ? @ ? tmp]? ./lastN.sh 01-glaube.mp3 output.mp3 1234
      Error: requested length is longer than the song.
      

      【讨论】:

        猜你喜欢
        • 2010-09-07
        • 2010-11-26
        • 2019-01-11
        • 2013-12-28
        • 2010-11-26
        • 1970-01-01
        • 2023-04-11
        • 2014-06-01
        • 2016-11-05
        相关资源
        最近更新 更多