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

C# / Whisper 모델 - 동영상의 음성을 인식해 자동으로 SRT 자막 파일을 생성

테스트를 위해 우선 동영상 파일 먼저 구해 볼까요? ^^

"C# - Youtube 동영상 다운로드 (YoutubeExplode 패키지)" 글에서 "https://youtu.be/90HFIm2Reqk" 동영상을 다운로드하면 비디오를 담은 mp4, 오디오를 담은 webm 파일과 그 2개를 합친 "...-final.mp4" 파일이 생성됩니다.

보통은 (video + audio가 모두 있는) mp4 파일만 있을 텐데요, 그런 경우에는 FFMpegCore를 사용해 mp4로부터 오디오를 분리해 내는 것부터 시작하면 됩니다.

// Install-Package FFMpegCore
using FFMpegCore;
string mp4FilePath = @"C:\temp\test_net_conf.mp4";
string mp3FilePath = @"C:\temp\test_net_conf.mp3"; // mp3 확장자 필수!

// ffmpeg -hide_banner -i test.webm -vn test.mp3
if (FFMpeg.ExtractAudio(mp4FilePath, mp3FilePath) == false)
{
    Console.WriteLine("invalid mp4 file");
    return;
}

/*
C:\temp> ffprobe -hide_banner test_net_conf.mp3
Input #0, mp3, from 'test_net_conf.mp3':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.45.100
  Duration: 00:40:59.57, start: 0.023021, bitrate: 128 kb/s
  Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.91
*/

이렇게 생성한 mp3 파일의 Sample Rate는 다양하겠지만, 위의 경우에는 48kHz로 나오는데요, 아쉽게도 Whisper 모델은 16kHz 규격만 처리할 수 있기 때문에 포맷을 변환해 주어야 합니다. 마침 NAudio에서 이런 변환 기능도 제공하므로,

examples/NAudioResampleWav/Program.cs
; https://github.com/sandrohanea/whisper.net/blob/main/examples/NAudioResampleWav/Program.cs

아래와 같이 mp3 파일을 전처리할 수 있습니다.

// Install-Package NAudio
using NAudio.Wave;
using var fileStream = File.OpenRead(mp3FilePath);
using var wavStream = new MemoryStream();

using var reader = new Mp3FileReader(fileStream);
var resampler = new WdlResamplingSampleProvider(reader.ToSampleProvider(), 16000);
WaveFileWriter.WriteWavFileToStream(wavStream, resampler.ToWaveProvider16());

wavStream.Seek(0, SeekOrigin.Begin);

자, 그럼 MemoryStream에 담긴 내용을 Whisper 모델에 넘겨주면 되는데요, 아무래도 GgmlType.Base 모델은 빠르긴 해도 정확도가 너무 떨어지므로, GgmlType.LargeV1 모델을 사용해 다음과 같은 식으로 음성을 텍스트로 변환할 수 있습니다.

// Install-Package Whisper.net.AllRuntimes

var ggmlType = GgmlType.LargeV1;
var modelFileName = "ggml-large-v1.bin";

if (!File.Exists(modelFileName))
{
    await DownloadModel(modelFileName, ggmlType);
}

using var whisperFactory = WhisperFactory.FromPath(modelFileName);

// This section creates the processor object which is used to process the audio file, it uses language `auto` to detect the language of the audio file.
using var processor = whisperFactory.CreateBuilder()
    .WithLanguage("auto")
    .Build();

// This section processes the audio file and prints the results (start time, end time and text) to the console.
await foreach (var result in processor.ProcessAsync(wavStream))
{
    Console.WriteLine($"{result.Start}->{result.End}: {result.Text}");
}

/* 출력 결과
00:00:00->00:00:06.9200000:  [모든 이야기는 다음 주에 만나요]
00:00:06.9200000->00:00:09.7200000:  [모든 이야기는 다음 주에 만나요]
00:00:09.7200000->00:00:14.1200000:  네, 시작하도록 하겠습니다.
00:00:14.1200000->00:00:17.8400000:  저는 이번 세션 발표를 맡은
...[생략]...
00:40:18.5200000->00:40:22.5200000:  이 패턴에 친숙해지시는 걸 추천드린다고 말씀드리고 싶습니다.
00:40:22.5200000->00:40:34.5200000:  제 발표는 여기까지였고요. 궁금하신 점이 있거나 하시면 이 링크드인, 이렇게 질문해 보세요라고 얘기를 하면 보통 질문을 안 하시더라고요.
00:40:34.5200000->00:40:44.5200000:  링크드인 주소가 있으니까 통해서 연락 주시면 제가 답해 드릴 수 있는 범위에서 최선을 다해서 답을 해 드리도록 하겠습니다.
00:40:44.5200000->00:40:46.5200000:  네, 고맙습니다.
*/

출력 예시를 보면, 훌륭하게도 ^^ 음성이 인식된 시점의 시작 시간과 종료 시간이 함께 표시되는 것을 알 수 있습니다. 즉, 자막 파일을 위한 기본적인 정보가 모두 갖추어진 셈입니다.

이제 남은 것은 자막 파일의 포맷에 맞게 쓰기만 하면 되는데요, 여러 자막 포맷이 있지만 여기서는 SRT 포맷을 예로 들어,

SRT 파일 구조 
; https://docs.fileformat.com/ko/video/srt/

/*
1
00:05:00,400 --> 00:05:15,300
This is an example of
a subtitle.

2
00:05:16,400 --> 00:05:25,300
This is an example of
a subtitle - 2nd subtitle.
*/

간단한 도우미 클래스를 만든 후,

using System.Text;

public class SRTWriter : IAsyncDisposable
{
    FileStream _fs;
    int _index;

    public SRTWriter(string filePath)
    {
        _index = 0;
        _fs = File.OpenWrite(filePath);
    }

    public void Write(TimeSpan startTime, TimeSpan endTime, string text)
    {
        _index++;

        string timeLine = $"{_index}\n{startTime:hh\\:mm\\:ss\\,fff} --> {endTime:hh\\:mm\\:ss\\,fff}\n";
        byte [] buffer = Encoding.UTF8.GetBytes(timeLine);
        _fs.Write(buffer);

        string subTitle = $"{text}\n\n";
        buffer = Encoding.UTF8.GetBytes(subTitle);
        _fs.Write(buffer);
    }

    public ValueTask DisposeAsync()
    {
        _fs.DisposeAsync();
        return ValueTask.CompletedTask;
    }
}

Whisper.NET의 출력을 그대로 보내주면 됩니다.

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

string srtPath = Path.ChangeExtension(mp4FilePath, ".srt");

File.WriteAllText(srtPath, string.Empty);

await using SRTWriter writer = new(srtPath);
await foreach (var result in processor.ProcessAsync(wavStream))
{
    Console.WriteLine($"{result.Start}->{result.End}: {result.Text}");
    writer.Write(result.Start, result.End, result.Text);
}

끝입니다. ^^ 실행해 보면, 가령 입력 파일이 test_net_conf.mp4 입력 파일이었다면 test_net_conf.srt 파일이 생성될 텐데요, 동영상 재생기를 통해 mp4 파일을 실행하면 다음과 같이 자막이 표시되는 것을 확인할 수 있습니다.

srt_from_video_1.png

게다가 Whisper 모델이 한국어뿐만 아니라 영어, 일본어 등을 지원하기 때문에, whisperFactory의 WithLanguage에 어떤 인자를 전달하느냐에 따라,

await using var processor = whisperFactory.CreateBuilder()
    .WithLanguage("ja") // 일본어로 번역
    .Build();

다국어 자막 파일을 (위의 경우에는 일본어로) 생성하는 것도 가능합니다.

srt_from_video_2.png

현재 GgmlType.LargeV1 모델 정도로도 꽤 괜찮은 인식률을 보여주기 때문에 이러한 방식으로 다국어 지원을 하는 것은 순전히 여러분이 소유한 GPU 성능에 달려 있다고 할 수 있습니다.

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




참고로, 아래의 동영상을 보면,

How I Transcribe Audio Locally with Whisper and .NET
; https://www.youtube.com/watch?v=H9kr7a78v44&ab_channel=StevanFreeborn

스티븐 프리본(Stevan Freeborn)이라는 분이 Whisper 모델을 사용해 동영상의 음성을 인식해 자막 파일을 생성하는 방법을 아주 상세히 설명해 주고 있습니다. 단지 그 영상의 소스 코드에서는 파일을 2분씩 끊어서 Whisper에 전달하고 있는데, 사실 그럴 필요가 없습니다. 왜냐하면 Azure OpenAI 서비스의 Whisper 모델에 대해서만 25MB 제약이 있는 것이고, 로컬에서 Whisper 모델을 사용하는 경우에는 그런 제약이 없기 때문입니다. 게다가 단순히 2분 단위로 끊으면 음성의 중간에 끊어질 수도 있기 때문에 가능하다면 단어 단위로 끊는 것이 더 좋습니다. ^^




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







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

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

비밀번호

댓글 작성자
 



2025-10-12 10시00분
umlx5h/LLPlayer
 - The media player for language learning, with dual subtitles, AI-generated subtitles, real-time translation, and more!
; https://github.com/umlx5h/LLPlayer
정성태

1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13924정성태5/8/20255627닷넷: 2332. C# - (JetBrains Omea Reader 대상으로) 런타임 시에 메서드 가로채기 [2]파일 다운로드1
13923정성태5/5/20254202스크립트: 74. 파이썬 - C# - Python.NET의 RunSimpleScript, Exec, Eval 차이점파일 다운로드1
13922정성태5/3/20255727스크립트: 73. 파이썬 - Windows embeddable package 버전에서 tkinter 환경 구성
13921정성태5/3/20255863오류 유형: 952. 듀얼 채널 메모리 정렬을 지키지 않은 컴퓨터의 Windows 비정상 종료 현상(Blue Screen) [2]
13920정성태5/3/20256500오류 유형: 951. Typed DataSet 생성 중 "Failed to open a connection to the database" 오류
13919정성태5/2/20255228VS.NET IDE: 201. C# - Typed DataSet(XSD)를 위한 연결 문자열 암호화 [1]파일 다운로드1
13918정성태5/2/20256533VS.NET IDE: 200. C# - app.config 파일의 출력을 Configuration(Debug/Release)에 따라 제어하는 방법파일 다운로드1
13917정성태4/30/20254861VS.NET IDE: 199. Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법
13916정성태4/23/20254853디버깅 기술: 221. WinDbg 분석 사례 - ASP.NET HttpCookieCollection을 다중 스레드에서 사용할 경우 무한 루프 현상 - 두 번째 이야기
13915정성태4/13/20256564닷넷: 2331. C# - 실행 시에 메서드 가로채기 (.NET 9)파일 다운로드1
13914정성태4/11/20257011디버깅 기술: 220. windbg 분석 사례 - x86 ASP.NET 웹 응용 프로그램의 CPU 100% 현상 (4)
13913정성태4/10/20254924오류 유형: 950. Process Explorer - 64비트 윈도우에서 32비트 프로세스의 덤프를 뜰 때 "Error writing dump file: Access is denied." 오류
13912정성태4/9/20254458닷넷: 2330. C# - 실행 시에 메서드 가로채기 (.NET 5 ~ .NET 8)파일 다운로드1
13911정성태4/8/20255477오류 유형: 949. WinDbg - .NET Core/5+ 응용 프로그램 디버깅 시 sos 확장을 자동으로 로드하지 못하는 문제
13910정성태4/8/20255017디버깅 기술: 219. WinDbg - 명령어 내에서 환경 변수 사용법
13909정성태4/7/20257239닷넷: 2329. C# - 실행 시에 메서드 가로채기 (.NET Framework 4.8)파일 다운로드1
13908정성태4/2/20257606닷넷: 2328. C# - MailKit: SMTP, POP3, IMAP 지원 라이브러리
13907정성태3/29/20258142VS.NET IDE: 198. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
13906정성태3/27/20257728닷넷: 2327. C# - 초기화되지 않은 메모리에 접근하는 버그?파일 다운로드1
13905정성태3/26/20257729Windows: 281. C++ - Windows / Critical Section의 안정화를 위해 도입된 "Keyed Event"파일 다운로드1
13904정성태3/25/20256948디버깅 기술: 218. Windbg로 살펴보는 Win32 Critical Section파일 다운로드1
13903정성태3/24/20256520VS.NET IDE: 197. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C++ 프로젝트의 출력 경로 변경하기
13902정성태3/24/20256577개발 환경 구성: 742. Oracle - 테스트용 hr 계정 및 데이터 생성파일 다운로드1
13901정성태3/9/20256966Windows: 280. Hyper-V의 3가지 Thread Scheduler (Classic, Core, Root)
13900정성태3/8/20258734스크립트: 72. 파이썬 - SQLAlchemy + oracledb 연동
13899정성태3/7/20255694스크립트: 71. 파이썬 - asyncio의 ContextVar 전달
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...