c# - FFmpeg.AutoGen 如何分割音频文件的例子

标签 c# c audio ffmpeg autogen

我想从这里使用 FFmpeg.AutoGen 项目:https://github.com/Ruslan-B/FFmpeg.AutoGen

我不熟悉 ffmpeg API,所以我想得到一个如何将音频文件拆分成小文件的示例,例如音频文件大约 2 小时长(可以是 mp3、ogg、wav、等),我想把它分成几个 x 分钟的小文件。分割应该在主音频文件上完成,时间戳从和到,例如从 = 00:25:00(意思是 25 分钟),到 = 00:31:12(意思是 31 分钟,12 秒)并且输出应该是主音频文件的部分,结果是 00:06:12(意思是 6 分 12 秒)长。

如何通过项目完成这项任务?还有一个 C 示例可以帮助我,我会尝试将其转换为框架。

感谢您的回复。

最佳答案

FFmpeg.AutoGen

我认为你需要做的:

  1. 解码音频文件
  2. 从所需的开始时间戳到所需的结束时间戳提取音频样本
  3. 对提取的样本进行编码
  4. 编写新的音频文件

这里有一些 C 语言的源代码,可以帮助您将它们放在一起:


我认为努力使用FFmpeg.AutoGen对于您的用例来说太高了,因此我建议 2 个替代方案:使用 NAudioFFmpeg通过命令行

NAudio

此代码读取 MP3,提取定义的片段并将其写入 WAV 文件。它基于博客文章 Concatenating Segments of an Audio File with NAudio并且可以很容易地进行调整。

using NAudio.Wave;
using System;

namespace NAudioSegments
{
    class SegmentProvider : IWaveProvider
    {
        private readonly WaveStream sourceStream;
        private int segmentStart, segmentDuration;

        public SegmentProvider(WaveStream sourceStream)
        {
            this.sourceStream = sourceStream;
        }

        public WaveFormat WaveFormat => sourceStream.WaveFormat;

        public void DefineSegment(TimeSpan start, TimeSpan duration)
        {
            if (start + duration > sourceStream.TotalTime)
                throw new ArgumentOutOfRangeException("Segment goes beyond end of input");
            segmentStart = TimeSpanToOffset(start);
            segmentDuration = TimeSpanToOffset(duration);
            sourceStream.Position = segmentStart;
        }

        public int TimeSpanToOffset(TimeSpan ts)
        {
            var bytes = (int)(WaveFormat.AverageBytesPerSecond * ts.TotalSeconds);
            bytes -= (bytes % WaveFormat.BlockAlign);
            return bytes;
        }

        public int Read(byte[] buffer, int offset, int count)
        {
            int totalBytesRead = 0;
            int bytesRead = 0;
            do
            {
                bytesRead = ReadFromSegment(buffer, offset + totalBytesRead, count - totalBytesRead);
                totalBytesRead += bytesRead;
            } while (totalBytesRead < count && bytesRead != 0);
            return totalBytesRead;
        }

        private int ReadFromSegment(byte[] buffer, int offset, int count)
        {
            var bytesAvailable = (int)(segmentStart + segmentDuration - sourceStream.Position);
            var bytesRequired = Math.Min(bytesAvailable, count);
            return sourceStream.Read(buffer, offset, bytesRequired);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var source = new Mp3FileReader(@"<input-path>"))
            {
                var segmentProvider = new SegmentProvider(source);
                // Add desired splitting e.g. start at 2 seconds, duration 1 second
                segmentProvider.DefineSegment(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1));
                WaveFileWriter.CreateWaveFile(@"<output-path>", segmentProvider);
            }
        }
    }
}

FFmpeg 通过命令行

您可以通过 System.Diagnostics.Process 直接从您的 C# 代码调用 ffmpeg类(参见例如 this SO question )而不是使用 FFmpeg.AutoGen。

然后您可以使用以下命令行自动将音频文件拆分为从 00:00:00 开始的相同长度的段

ffmpeg -i in.m4a -f segment -segment_time 300 -c copy out%03d.m4a

或者您可以使用参数 -ss 更改开始时间(将 <start-time> 替换为秒数)。您需要为每个 segmentation 市场重复此操作。

ffmpeg -ss <start-time> -i in.m4a -c copy -t 300 out.m4a

Source on superuser

关于c# - FFmpeg.AutoGen 如何分割音频文件的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963036/

相关文章:

python - 如何使用python处理音频文件

audio - WAV "data"子 block 是如何构成的?

c# - 在监 window 口中评估动态属性

C# 将 Foreach 值转换为其属性之一

c - 如何在 C 中逐行读取 .txt?

java - 线程不释放内存

c# - 阻止按键事件冒泡的正确方法

c# - 自定义 xsd.exe 工具和区分大小写的 xml 序列化

c - 使用内联汇编循环遍历数组

c - 为许多 C 库实现标准接口(interface)