c# – 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语言中的一些来源,可以帮助您将所有内容组合在一起: >读取任何音频文件类型:Libavcodec tutorial: decode virtually any audio file in C/C++.注意:avcodec_decode_audio4已被弃用(请参阅New AVCodec API)您应该使用avcodec_send_packet / avcodec_receive_frame. 我认为使用FFmpeg.AutoGen的努力对于你的用例来说太高了,因此我提出了两种选择:通过命令行使用NAudio或FFmpeg n音讯 此代码读取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 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开始的相同长度的segemnts中的音频文件 ffmpeg -i in.m4a -f segment -segment_time 300 -c copy out%03d.m4a 或者您可以使用参数-ss更改开始时间(将< start-time>替换为秒数).您需要为每个细分重复此操作. ffmpeg -ss <start-time> -i in.m4a -c copy -t 300 out.m4a Source on superuser (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |