Sve*_*ven 3 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
我认为你需要做的事情:
以下是C中的一些来源,可能有助于您将所有内容组合在一起:
avcodec_decode_audio4已弃用(请参阅新的AVCodec API)您应该使用avcodec_send_packet/ avcodec_receive_frame.decode_audio.c/encode_audio.c我认为使用的努力FFmpeg.AutoGen对于您的用例来说太高了,因此我提出了两种选择:使用NAudio或FFmpeg通过命令行
n音讯
此代码读取MP3,提取已定义的段并将其写入WAV文件.它基于博客文章与NAudio的音频文件的Concatenating Segments,可以很容易地调整.
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
FFmpeg通过命令行
你可以直接从你的C#代码通过调用的ffmpeg System.Diagnostics.Process类(例如参见这太问题),而不是使用FFmpeg.AutoGen.
然后,您可以使用以下命令行自动拆分从00:00:00开始的相同长度的segemnts中的音频文件
ffmpeg -i in.m4a -f segment -segment_time 300 -c copy out%03d.m4a
Run Code Online (Sandbox Code Playgroud)
或者您可以使用参数更改开始时间-ss(替换<start-time>为秒数).您需要为每个细分重复此操作.
ffmpeg -ss <start-time> -i in.m4a -c copy -t 300 out.m4a
Run Code Online (Sandbox Code Playgroud)