如何使用 ffmpeg 将 mp3 从 x 裁剪为 x+n?

发布于 2024-08-04 06:49:02 字数 528 浏览 10 评论 0原文

这个问题之后我决定使用ffmpeg进行裁剪MP3。在另一个问题上,我发现了这样做的方法:

ffmpeg -t 30 -acodec copy -i inputfile.mp3 outputfile.mp3

问题是我不想裁剪前 30 秒,我想从 x 裁剪到 x+n,比如从 30 秒裁剪到 100 秒。我该怎么做呢?

我正在阅读 man for ffmpeg 但这并不是很简单,特别是因为我只是发现了 ffmpeg 并且我不熟悉音频/视频编辑软件,因此任何指示将不胜感激。

Following this question I decided to use ffmpeg to crop MP3s. On another question I found this way of doing it:

ffmpeg -t 30 -acodec copy -i inputfile.mp3 outputfile.mp3

The problem is that I don't want to crop the first 30 seconds, I want to crop from x to x+n, like from 30s to 100s. How would I go and do this?

I'm reading the man for ffmpeg but this is not really straightforward, especially since I just discovered about ffmpeg and I'm not familiar with audio/video editing softwares, so any pointers would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-08-11 06:49:02

看一下 -t 和 -ss 参数。他们应该做你想做的事。

-t 持续时间

限制转码/捕获的视频
序列到指定的持续时间
秒。 hh:mm:ss[.xxx] 语法也是
支持。

-SS位置'

寻找给定的时间位置(以秒为单位)。
还支持 hh:mm:ss[.xxx] 语法。

例如, ffmpeg -ss 30 -t 70 -i inputfile.mp3 -acodec copy outputfile.mp3 应该可以满足您提到的范围(30s-100s)。

Take a look at the -t and -ss arguments. They should do what you want.

-t duration

Restrict the transcoded/captured video
sequence to the duration specified in
seconds. hh:mm:ss[.xxx] syntax is also
supported.

-ss position'

Seek to given time position in seconds.
hh:mm:ss[.xxx] syntax is also supported.

For example, ffmpeg -ss 30 -t 70 -i inputfile.mp3 -acodec copy outputfile.mp3 should do the trick for the range you mentioned (30s-100s).

迷迭香的记忆 2024-08-11 06:49:02

稍微扩展一下 Michael Madsens 的回答:

我发现以下任一方法都可以满足修剪音频文件的要求:

  • ffmpeg -ss<开始位置>-t< ;duration>-i inputfile -c:复制输出文件
  • ffmpeg -ss<开始位置>-i inputfile -t; -c:a copy outputfile

注意:-acodeccodec:a 的别名,也可以指定为 c:a

主要选项 FFMPEG 文档中指定

-t 持续时间(输入/输出)

  • 当用作输入选项(在 -i 之前)时,限制从输入文件读取数据的持续时间。
  • 当用作输出选项时(在输出 url 之前),在其持续时间达到持续时间后停止写入输出。

-ss 位置(输入/输出)

  • 当用作输入选项(在 -i 之前)时,在此输入文件中查找位置。
  • 当用作输出选项时(在输出 url 之前),解码但丢弃输入,直到时间戳到达位置。

durationposition 遵循 持续时间语法

  • [-][HH:]MM:SS[.m...][-]S+[.m...][s| ms|us]

旁注:如何使用 FFMPEG 检测静音间隔的答案可能也很有趣。


作为输入或输出的搜索和持续时间之间的差异

请注意,在大多数格式中不可能精确查找,因此 ffmpeg 将在位置之前查找最近的查找点。当转码和 -accurate_seek 启用(默认)时,搜索点和位置之间的额外段将被解码并丢弃。当进行流复制或使用-noaccurate_seek时,它将被保留。

我对以下文件做了几次测试:

Input #0, mp3, from 'test16s.mp3':
  Duration: 00:00:16.20, start: 0.025057, bitrate: 128 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s

与原始文件相比,在 Audacity 中对齐输出并得到以下结果:

  • ffmpeg -ss 3 -t 5 -i test16s.mp3 -c:a copy out.mp3
    • 开始:3s+00756samples (3.017142857s)
    • 结束:8s+02062samples (8.04675737s)
    • 持续时间:5 秒+01306 个样本(5.029614512 秒)

  • ffmpeg -ss 3 -i test16s.mp3 -t 5 -c:a copy out.mp3
    • 开始:3s+00756samples (3.017142857s)
    • 结束:8s+00910 个样本(8.020634921s)
    • 持续时间:5 秒+00154 个样本(5.003492063 秒)
  • ffmpeg -i test16s.mp3 -ss 3 -t 5 -c:a copy out.mp3
    • 警告:这会创建一个文件,某些 mp3 解码器在尝试打开时会出错。
    • 开始:2s+42184samples (2.956553288s)
    • 结束:8 秒+01071 个样本(8.024285714 秒)
    • 持续时间:5 秒+02987 个样本(5.067732426 秒)
  • ffmpeg -t 5 -i test16s.mp3 -ss 3 -c:a copy out.mp3
    • 警告:这会创建一个文件,某些 mp3 解码器在尝试打开时会出错。
    • 开始:2s+42184samples (2.956553288s)
    • 结束:5 秒+02043 个样本(5.046326531 秒)
    • 持续时间:2s+03959 个样本 (2.089773243)

在尝试查看搜索跳转时,我发现了有趣的结果
使用:

  • ffmpeg -ss <秒> -i test16s.mp3 -t 5 -c:a copy out.mp3

    • <秒> = 3, 2.99, 2.98, 2.97
      • 开始:3s+00756samples (3.017142857s),持续时间:5s+00154
    • <秒> = 2.96
      • 开始:同上,持续时间:4s+41951
    • <秒> = 2.95
      • 开始:2s+43704 (2.991020408),结束:7s+42707 持续时间:4s+43103
    • <秒> = 2.94,2.93
      • 开始:同上,持续时间:4s+41951
    • <秒> = 2.92
      • 开始:2s+42552 (2.964897959s),持续时间:4s+43103
  • ffmpeg -ss-t 5 -i test16s.mp3 -c:a copy out.mp3

    • <秒> = 3
      • 开始:3s+00756 (3.017142857s),持续时间:5s+01306 (5.029614512s)
    • <秒> = 2.99, 2.98, 2.97
      • 开始:相同,持续时间:5s+00155
    • <秒> = 2.96
      • 开始:相同,持续时间:4s+43103
    • <秒> = 2.95
      • 开始:2s+43704 (2.991020408),结束:7s+43859 持续时间:5s+00155
    • <秒> = 2.94,2.93
      • 开始:相同,持续时间:4s+43103
    • <秒> = 2.92
      • 开始:2s+42552 (2.964897959s),持续时间:5s+00155

最后,使用流复制,看起来就像我的特定文件中的最小搜索分辨率一样,(音频:mp3,44100 Hz、立体声、fltp、128 kb/s),为:

  • 寻道步骤:1152 个样本,26.122449 ms
  • 寻道位置 误差高达 70 ms

To expand a bit on Michael Madsens' Answer:

I've found either of the following satisfactory for trimming my audio files:

  • ffmpeg -ss<start position>-t<duration>-i inputfile -c:a copy outputfile
  • ffmpeg -ss<start position>-i inputfile -t<duration>-c:a copy outputfile

Note: -acodec is an alias for codec:a which can be specified also as c:a

As specified in the Main Options FFMPEG Documentation

-t duration (input/output)

  • When used as an input option (before -i), limit the duration of data read from the input file.
  • When used as an output option (before an output url), stop writing the output after its duration reaches duration.

-ss position (input/output)

  • When used as an input option (before -i), seeks in this input file to position.
  • When used as an output option (before an output url), decodes but discards input until the timestamps reach position.

duration and position follow the Time Duration Syntax :

  • [-][HH:]MM:SS[.m...] or [-]S+[.m...][s|ms|us]

Side Note: An answer on How to detect intervals of silence with FFMPEG may also be of interest.


Differences between seeking and duration as input or output

Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

I did a couple Tests on the following file:

Input #0, mp3, from 'test16s.mp3':
  Duration: 00:00:16.20, start: 0.025057, bitrate: 128 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s

Aligned the outputs in Audacity in comparison to the original and got the following:

  • ffmpeg -ss 3 -t 5 -i test16s.mp3 -c:a copy out.mp3
    • Start: 3s+00756samples (3.017142857s)
    • End: 8s+02062samples (8.04675737s)
    • Duration : 5s+01306samples (5.029614512s)
  • ffmpeg -ss 3 -i test16s.mp3 -t 5 -c:a copy out.mp3
    • Start: 3s+00756samples (3.017142857s)
    • End: 8s+00910samples (8.020634921s)
    • Duration : 5s+00154samples (5.003492063s)
  • ffmpeg -i test16s.mp3 -ss 3 -t 5 -c:a copy out.mp3
    • Warning: This created a file that some mp3 decoders gave error on attempting to open.
    • Start: 2s+42184samples (2.956553288s)
    • End: 8s+01071samples (8.024285714s)
    • Duration : 5s+02987samples (5.067732426s)
  • ffmpeg -t 5 -i test16s.mp3 -ss 3 -c:a copy out.mp3
    • Warning: This created a file that some mp3 decoders gave error on attempting to open.
    • Start: 2s+42184samples (2.956553288s)
    • End: 5s+02043samples (5.046326531s)
    • Duration : 2s+03959samples (2.089773243)

In an attempt to get to see the seek jump I found interesting results
using :

  • ffmpeg -ss <secs> -i test16s.mp3 -t 5 -c:a copy out.mp3

    • <secs> = 3, 2.99, 2.98, 2.97
      • Start: 3s+00756samples (3.017142857s), Duration:5s+00154
    • <secs> = 2.96
      • Start: same as above, Duration: 4s+41951
    • <secs> = 2.95
      • Start: 2s+43704 (2.991020408), End:7s+42707 Duration:4s+43103
    • <secs> = 2.94,2.93
      • Start: same as above, Duration: 4s+41951
    • <secs> = 2.92
      • Start: 2s+42552 (2.964897959s), Duration: 4s+43103
  • ffmpeg -ss <secs> -t 5 -i test16s.mp3 -c:a copy out.mp3

    • <secs> = 3
      • Start: 3s+00756 (3.017142857s), Duration:5s+01306 (5.029614512s)
    • <secs> = 2.99, 2.98, 2.97
      • Start: same, Duration: 5s+00155
    • <secs> = 2.96
      • Start: same, Duration: 4s+43103
    • <secs> = 2.95
      • Start: 2s+43704 (2.991020408), End:7s+43859 Duration:5s+00155
    • <secs> = 2.94,2.93
      • Start: same, Duration: 4s+43103
    • <secs> = 2.92
      • Start: 2s+42552 (2.964897959s), Duration: 5s+00155

Concluding that with a Stream Copy, appears as if the minimal seek resolution in my specific file ,(Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s), was:

  • Seek Step of : 1152 samples, 26.122449 ms
  • Seek position Error of up to 70 ms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文