Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일

C# / NAudio - (AI 학습을 위해) 무음 구간을 반영한 오디오 파일 분할

Whisper 모델의 경우,

C# - Whisper.NET Library를 이용해 음성을 텍스트로 변환 및 번역하는 예제
; https://www.sysnet.pe.kr/2/0/14013

로컬에서 실행하는 경우에는 문제가 없지만, Azure OpenAI 서비스의 Whisper 모델을 이용하는 경우에는 25MB 파일 크기 제한이 있습니다. 그렇다면 일정 크기로 나눠야 할 텐데요, 하지만 음성 데이터의 특성상 단순히 파일 크기로 나누기보다는 무음 구간을 인지해 나누는 것이 음성 인식의 정확도를 높이는 데 도움이 됩니다.

자, 그래서 ^^ 이번에는 무음 구간을 반영한 오디오 청크 분할 방법에 대해 알아보겠습니다.




이를 위해 우선 적절한 동영상 파일이 있어야 하는데요, 마침 Youtube로부터 다운로드하는 것도 만들었으니,

C# - Youtube 동영상 다운로드 (YoutubeExplode 패키지)
; https://www.sysnet.pe.kr/2/0/14021

먼저 이것을 시간에 따라 청크로 나눠보겠습니다. NAudio 라이브러리의 경우 특정 시간만을 잘라내는 기능을 이미 제공하고 있으므로,

Using OffsetSampleProvider
; https://github.com/naudio/NAudio/blob/master/Docs/OffsetSampleProvider.md

using var reader = new AudioFileReader("test.wav");

var offset = new OffsetSampleProvider(reader);
offset.SkipOver = TimeSpan.FromSeconds(10); // 5초 후,
offset.Take = TimeSpan.FromSeconds(20); // 20초 분량만큼.

// 이 구간만을 잘라내서 out.wav로 저장.
WaveFileWriter.CreateWaveFile16(@"out.wav", offset);

특정 오디오 파일을 (예를 들어) 2분마다 끊어서 파일에 저장하는 것을 다음과 같이 구현할 수 있습니다.

using var reader = new AudioFileReader(inputPath);

var chunkDuration = TimeSpan.FromMinutes(2);
var numOfChunks = (int)Math.Ceiling(reader.TotalTime.TotalSeconds / chunkDuration.TotalSeconds);

for (int i = 0; i < numOfChunks; i++)
{
    reader.Position = 0;
    var offset = new OffsetSampleProvider(reader);
    offset.SkipOver = TimeSpan.FromSeconds(i * chunkDuration.TotalSeconds);
    offset.Take = TimeSpan.FromSeconds(chunkDuration.TotalSeconds);

    WaveFileWriter.CreateWaveFile16($"out_{i}min.wav", offset);
}




하지만 우리가 원하는 것은, 단순한 파일 크기만이 아니라 거기에 무음 구간을 반영하는 것입니다. 이를 위해서는 어쨌든 음성 데이터에 접근해야 하는데요, NAudio의 경우 샘플링된 데이터를 다음과 같은 방식으로 열거할 수 있습니다.

AudioFileReader source = new("...[오디오 파일 경로]...");

source.Position = 0;

float[] buffer = new float[1600];
long totalSamplesRead = 0;
int read;

while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
    for (int i = 0; i < read; i++)
    {
        float value = buffer[i]; // -1.0 ~ +1.0 사이의 값
    }

    totalSamplesRead += read;
}

엄밀히 WAV 데이터는 bitsPerSample의 값 범위를 갖는데, 예를 들어 16비트라면 (-32768도 표현은 되지만) -32767 ~ +32767 사이의 값이 됩니다. 반면 NAudio가 열거한 값은 -1.0 ~ +1.0 사이의 정규화된 값이므로 이상적인 무음 값은 0.0이 연속으로 나와야 합니다.

하지만, 아날로그 성격상 오디오 데이터는 잡음이 섞일 수 있기 때문에, 가령 Abs(0.01) 이하의 진동만 있다면 무음이라는 식으로 판단해야 합니다. 그리고 한 가지 더 고려해야 할 것이, "무음의 연속"에 대한 판단입니다. 0.01 이하의 값은 샘플링된 순간에 따라 나오는 것도 가능하기 때문에 어느 정도 지속 구간을 함께 기준으로 추가해야 합니다.

이를 바탕으로 코딩을 하려고 했는데... 요즘이 어떤 세상입니까? ^^ AI가 코드 생성을 이렇게 해주는군요.

static List<TimeSpan> DetectSilencePoints(
        AudioFileReader source,
        double silenceThresholdDb = -35,
        int minSilenceDurationMs = 300,
        int analysisWindowMs = 50)
{
    // Reset reader to start
    source.Position = 0;

    int sampleRate = source.WaveFormat.SampleRate;
    int channels = source.WaveFormat.Channels;
    int samplesPerWindow = Math.Max(1, sampleRate * analysisWindowMs / 1000) * channels;

    float[] buffer = new float[samplesPerWindow];
    var silenceRuns = new List<(TimeSpan start, TimeSpan end)>();

    bool inSilence = false;
    TimeSpan runStart = TimeSpan.Zero;

    long totalSamplesRead = 0;
    int read;

    while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        int frames = read / channels;
        if (frames <= 0) continue;

        // Peak of this window
        float peak = 0f;
        for (int i = 0; i < read; i++)
        {
            peak = Math.Max(peak, Math.Abs(buffer[i]));
        }

        // Convert to dBFS; clamp to avoid log(0)
        double db = (peak <= 1e-9) ? -120.0 : 20.0 * Math.Log10(peak);

        TimeSpan windowStart = SamplesToTime(totalSamplesRead, sampleRate, channels);
        TimeSpan windowEnd = SamplesToTime(totalSamplesRead + read, sampleRate, channels);

        bool isSilent = db <= silenceThresholdDb;

        if (isSilent && !inSilence)
        {
            inSilence = true;
            runStart = windowStart;
        }
        else if (!isSilent && inSilence)
        {
            inSilence = false;
            var dur = windowStart - runStart;
            if (dur.TotalMilliseconds >= minSilenceDurationMs)
                silenceRuns.Add((runStart, windowStart));
        }

        totalSamplesRead += read;
    }

    // End boundary
    if (inSilence)
    {
        var end = SamplesToTime(totalSamplesRead, sampleRate, channels);
        var dur = end - runStart;
        if (dur.TotalMilliseconds >= minSilenceDurationMs)
            silenceRuns.Add((runStart, end));
    }

    // Use midpoints of runs as candidate "clean" cut points.
    var points = silenceRuns.Select(run => run.start + TimeSpan.FromTicks((run.end - run.start).Ticks / 2)).ToList();

    Console.WriteLine($"Detected {points.Count} silence candidates (≥ {minSilenceDurationMs} ms @ {silenceThresholdDb} dBFS).");
    return points;
}

그러니까, 총 300ms 구간 동안 -35dBFS 이하의 값이 연속으로 나오면 그것을 무음으로 판단하고 있습니다. 개인적으로 "dBFS"라는 단위를 처음 봤는데요, 코드에서처럼 정규화된 PCM 값에 아래의 공식을 적용해 계산할 수 있다고 합니다.

dBFS = 20 * log10(정규화된 값)

그런 의미에서 봤을 때, -35dBFS는 대략 0.0177 정도의 진폭에 해당하기 때문에 저 계산을 그냥 빼고 진폭 값을 직접 비교해도 무방합니다.

static List<TimeSpan> DetectSilencePoints(
        AudioFileReader source,
        double silenceThreshold = 0.0177f,
        int minSilenceDurationMs = 300,
        int analysisWindowMs = 50)
{
    // ...[생략]...

    while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        // ...[생략]...

        // Peak of this window
        float peak = 0f;
        for (int i = 0; i < read; i++)
        {
            peak = Math.Max(peak, Math.Abs(buffer[i]));
        }

        // 0.1 ==> 약 -40dBFS
        // 0.0177 ==> 약 -35.04dBFS

        // 주석 처리
        // double db = peak; // (peak <= 1e-9) ? -120.0 : 20.0 * Math.Log10(peak);

        TimeSpan windowStart = SamplesToTime(totalSamplesRead, sampleRate, channels);
        TimeSpan windowEnd = SamplesToTime(totalSamplesRead + read, sampleRate, channels);

        bool isSilent = peak <= silenceThreshold;

        // ...[생략]...
    }

    // ...[생략]...

    return points;
}




자, 이제 마지막으로 무음 구간을 반영해 청크로 나눠 볼 텐데요, 가령 2분 단위로 끊되 앞/뒤로 무음 구간이 있다면 그걸 반영해 끊는 것입니다. 이것도 그냥 AI한테 만들어 달라고 하니 다음과 같이 ^^ 잘 만들어줍니다.

/// <summary>
/// Compute final cut points: start(0), snapped cuts near each target interval, end(total).
/// </summary>
static List<TimeSpan> ComputeCutPoints(
    TimeSpan total,
    TimeSpan targetInterval,
    List<TimeSpan> silenceCandidates,
    TimeSpan searchWindow,
    TimeSpan minGapAfterCut)
{
    var cuts = new List<TimeSpan> { TimeSpan.Zero };

    // Generate target times: 2min, 4min, ... < total
    var targets = new List<TimeSpan>();
    for (var t = targetInterval; t < total; t += targetInterval)
        targets.Add(t);

    TimeSpan lastCut = TimeSpan.Zero;

    foreach (var t in targets)
    {
        var min = t - searchWindow;
        var max = t + searchWindow;

        // Find nearest silence within window that keeps reasonable spacing
        var candidate = silenceCandidates
            .Where(s => s >= min && s <= max && (s - lastCut) >= minGapAfterCut)
            .OrderBy(s => Math.Abs((s - t).Ticks))
            .FirstOrDefault();

        TimeSpan chosen;
        if (candidate == default)
        {
            chosen = (t - lastCut) >= minGapAfterCut ? t : lastCut; // fallback to exact t if spacing ok
        }
        else
        {
            chosen = candidate;
        }

        if (chosen > lastCut && chosen < total)
        {
            cuts.Add(chosen);
            lastCut = chosen;
        }
    }

    if (cuts.Last() != total)
        cuts.Add(total);

    // Deduplicate / sort just in case
    cuts = cuts.Distinct().OrderBy(ts => ts).ToList();

    Console.WriteLine("Cut map:");
    for (int i = 0; i < cuts.Count; i++)
        Console.WriteLine($"  [{i}] {cuts[i]}");

    return cuts;
}

딱히 어려운 메서드는 아니므로 설명은 생략하겠습니다. ^^ 여기까지의 코드를 종합하면,

string inputPath = @"C:\media_sample\test.wav";

using var reader = new AudioFileReader(inputPath);

// 1) Detect silence points.
List<TimeSpan> silencePoints = DetectSilencePoints(reader, silenceThresholdDb: 0.0177, minSilenceDurationMs: 300, analysisWindowMs: 50);

double segmentSec = 120;
double searchWindowSec = 15;

// 2) Build cut list: 0, cuts near 2min multiples (snapped to silence), end
List<TimeSpan> cutPoints = ComputeCutPoints(
    total: reader.TotalTime,
    targetInterval: TimeSpan.FromSeconds(segmentSec),
    silenceCandidates: silencePoints,
    searchWindow: TimeSpan.FromSeconds(searchWindowSec),
    minGapAfterCut: TimeSpan.FromSeconds(5) // avoid super-nearby duplicate cuts
);

우리가 원하는 오디오 구간을 반영한 TimeSpan 목록을 얻게 됩니다.




끝이군요, ^^ TimeSpan 목록을 얻었으니 이제 그에 맞게 오디오 청크를 (처음에 설명했던 OffsetSampleProvider를 이용해) 잘라내는 것으로 마무리할 수 있습니다.

string outputDir = Path.Combine(Environment.CurrentDirectory, "output");
Directory.CreateDirectory(outputDir);
ExportSegments(inputPath, outputDir, cutPoints, reader);

static void ExportSegments(string inputPath, string outputDir, List<TimeSpan> cuts, AudioFileReader reader)
{
    string baseName = Path.GetFileNameWithoutExtension(inputPath);

    for (int i = 0; i + 1 < cuts.Count; i++)
    {
        var start = cuts[i];
        var end = cuts[i + 1];
        var len = end - start;
        if (len <= TimeSpan.Zero) continue;

        reader.Position = 0;
        var offset = new OffsetSampleProvider(reader)
        {
            SkipOver = start,
            Take = len,
        };

        string outPath = Path.Combine(outputDir, $"{baseName}_part_{i + 1:00}_{FormatTime(start)}~{FormatTime(end)}.wav");
        WaveFileWriter.CreateWaveFile16(outPath, offset); // writes 16-bit WAV
        Console.WriteLine($"Wrote: {outPath} ({len})");
    }
}

제가 테스트한 "https://youtu.be/90HFIm2Reqk" 40여 분 정도의 동영상은 다음과 같이 21개의 청크로 나누어졌고,

Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_01_00-00-00~00-01-58.wav (00:01:58.7000000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_02_00-01-58~00-03-56.wav (00:01:57.7000000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_03_00-03-56~00-06-06.wav (00:02:09.7750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_04_00-06-06~00-07-57.wav (00:01:51.6500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_05_00-07-57~00-10-00.wav (00:02:02.8250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_06_00-10-00~00-12-12.wav (00:02:11.5500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_07_00-12-12~00-14-01.wav (00:01:48.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_08_00-14-01~00-15-59.wav (00:01:57.9500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_09_00-15-59~00-18-09.wav (00:02:10.5250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_10_00-18-09~00-19-54.wav (00:01:45.2250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_11_00-19-54~00-22-00.wav (00:02:05.6500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_12_00-22-00~00-24-06.wav (00:02:06.2250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_13_00-24-06~00-25-57.wav (00:01:50.5750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_14_00-25-57~00-28-00.wav (00:02:03.0250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_15_00-28-00~00-30-07.wav (00:02:07.0500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_16_00-30-07~00-32-05.wav (00:01:57.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_17_00-32-05~00-34-02.wav (00:01:57.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_18_00-34-02~00-36-08.wav (00:02:05.1750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_19_00-36-08~00-38-00.wav (00:01:51.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_20_00-38-00~00-39-55.wav (00:01:55.5250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_21_00-39-55~00-40-59.wav (00:01:03.9950000)

몇몇 음성 파일의 끝과 그다음 시작 부분을 들어보니 무음 구간을 잘 반영해 끊어진 것을 알 수 있었습니다. ^^

(첨부 파일은 이 글의 소스 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 10/10/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13723정성태9/2/202412158C/C++: 170. Windows - STARTUPINFO의 cbReserved2, lpReserved2 멤버 사용자 정의파일 다운로드2
13722정성태9/2/20249772C/C++: 169. C/C++ - CRT(C Runtime) 함수에 의존성이 없는 프로젝트 생성
13721정성태8/30/20249339C/C++: 168. Visual C++ CRT(C Runtime DLL: msvcr...dll)에 대한 의존성 제거 - 두 번째 이야기
13720정성태8/29/202410190VS.NET IDE: 193. C# - Visual Studio의 자식 프로세스 디버깅
13719정성태8/28/20249713Linux: 79. C++ - pthread_mutexattr_destroy가 없다면 메모리 누수가 발생할까요?
13718정성태8/27/202412483오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
13717정성태8/26/202412381VS.NET IDE: 192. Visual Studio 2022 - Windows XP / 2003용 C/C++ 프로젝트 빌드
13716정성태8/21/202410539C/C++: 167. Visual C++ - 윈도우 환경에서 _execv 동작 [1]
13715정성태8/19/202412908Linux: 78. 리눅스 C/C++ - 특정 버전의 glibc 빌드 (docker-glibc-builder)
13714정성태8/19/202410495닷넷: 2295. C# 12 - 기본 생성자(Primary constructors) (책 오타 수정) [3]
13713정성태8/16/202413265개발 환경 구성: 721. WSL 2에서의 Hyper-V Socket 연동
13712정성태8/14/202411517개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/202412286Linux: 77. C# / Linux - zombie process (defunct process) [1]파일 다운로드1
13710정성태8/8/202413417닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/202411648닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/202412534개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/202412061닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/202411341개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/202411691닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용 [2]파일 다운로드1
13704정성태8/2/202413381닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/202411451닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/202412805닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/202413779닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/202413443디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/202413959닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/202412847닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...