Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 8개 있습니다.)
.NET Framework: 1140. C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP3 오디오 파일 인코딩/디코딩하는 예제
; https://www.sysnet.pe.kr/2/0/12939

.NET Framework: 1144. C# - ffmpeg(FFmpeg.AutoGen) AVFormatContext를 이용해 ffprobe처럼 정보 출력
; https://www.sysnet.pe.kr/2/0/12948

.NET Framework: 1145. C# - ffmpeg(FFmpeg.AutoGen) - Codec 정보 열람 및 사용 준비
; https://www.sysnet.pe.kr/2/0/12949

.NET Framework: 1148.  C# - ffmpeg(FFmpeg.AutoGen) - decoding 과정
; https://www.sysnet.pe.kr/2/0/12956

.NET Framework: 1149. C# - ffmpeg(FFmpeg.AutoGen) - 비디오 프레임 디코딩
; https://www.sysnet.pe.kr/2/0/12958

.NET Framework: 1155. C# - ffmpeg(FFmpeg.AutoGen): Bitmap으로부터 yuv420p + rawvideo 형식의 파일로 쓰기
; https://www.sysnet.pe.kr/2/0/12966

.NET Framework: 1156. C# - ffmpeg(FFmpeg.AutoGen): Bitmap으로부터 h264 형식의 파일로 쓰기
; https://www.sysnet.pe.kr/2/0/12970

.NET Framework: 1160. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 qsv 디코딩
; https://www.sysnet.pe.kr/2/0/12977




C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP3 오디오 파일 인코딩/디코딩하는 예제

지난 글에서,

C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP2 오디오 파일 디코딩 예제(decode_audio.c)
; https://www.sysnet.pe.kr/2/0/12933

C# - ffmpeg(FFmpeg.AutoGen)를 이용해 오디오(mp2) 인코딩하는 예제(encode_audio.c)
; https://www.sysnet.pe.kr/2/0/12937

MP2 파일을 인코딩/디코딩했는데요, 그렇다면 MP3 파일은 어떨까요? 일단 인코딩 먼저 볼까요? ^^

지난 소스 코드를 참고해, 단순히 인코더만 MP3로 선택하면,

codec = ffmpeg.avcodec_find_encoder(AVCodecID.AV_CODEC_ID_MP3);

이후 ffmpeg.av_frame_get_buffer 호출에서 -22를 반환합니다. (소스 코드 내에서의 오류 메시지: "Could not allocate audio data buffers")

[mp3_mf @ 00000288337D7B00] MFT name: 'MP3 Encoder ACM Wrapper MFT'
Could not allocate audio data buffers

함께 출력된 로그를 보면 "MP3 Encoder ACM Wrapper MFT"라고 나오는데, 검색해 보면 MFT는 Media Foundation Transform의 약자로, 아마도 마이크로소프트의 운영체제에 포함된 내장 인코더로 보입니다.

MP3 Audio Encoder
; https://docs.microsoft.com/en-us/windows/win32/medfound/mp3-audio-encoder

음... 저 인코더는 뭔가 특별한 게 있는 걸까요? ^^;




혹시나 싶어 AV_CODEC_ID_MP3 검색어로 찾아봤더니,

ffmpeg-toys/avcodec.c
; https://github.com/joelanders/ffmpeg-toys/blob/master/avcodec.c

예제가 나옵니다. 하지만 이 예제 코드는 av_frame_get_buffer 호출까지 가기도 전에 이미 check_sample_fmt에서 걸립니다.

c->sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16P;
if (check_sample_fmt(codec, c->sample_fmt) == 0)
{
    Console.WriteLine($"Encoder does not support sample format {ffmpeg.av_get_sample_fmt_name(c->sample_fmt)}");
    break;
}

check_sample_fmt 함수의 내부를 디버깅해 보면 ("MP3 Encoder ACM Wrapper MFT" 코덱이) AV_SAMPLE_FMT_S16만 지원한다는 것을 알 수 있습니다. (어쨌든, 저 코드를 작성한 개발자의 PC에서는 잘 동작했을 것입니다.)

게다가 그 외의 "ffmpeg-toys/avcodec.c" 코드가 이전 mp2 인코딩과 다른 걸로 봐서는 아마도 deprecated 함수로 구성이 된 게 아닌가 싶습니다.




좀 뚝딱거리다 보니, 해당 코덱의 경우 avcodec_open2 호출 이후에 c->frame_size가 0이라는 점이 독특합니다. 왜냐하면, MP2 예제에서는 1152 값이 나왔기 때문입니다.

아마도 이것 때문에 이후 av_samples_get_buffer_size, av_frame_get_buffer 등의 호출에서 반환값이 -22가 나온 듯합니다.

ffmpeg.AVERROR(ffmpeg.EAGAIN); // -11
ffmpeg.AVERROR_EOF; // -541478725
ffmpeg.AVERROR(ffmpeg.EINVAL); // -22
ffmpeg.AVERROR(ffmpeg.ENOMEM); // -12
ffmpeg.AVERROR(ffmpeg.EPIPE); // -32

실제로 av_samples_get_buffer_size의 소스 코드를 보면,

// https://ffmpeg.org/doxygen/trunk/samplefmt_8c_source.html#l00119
 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
                                enum AVSampleFormat sample_fmt, int align)
 {
     int line_size;
     int sample_size = av_get_bytes_per_sample(sample_fmt);
     int planar      = av_sample_fmt_is_planar(sample_fmt);
  
     /* validate parameter ranges */
     if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
         return AVERROR(EINVAL);
  
     ...[생략]...
 }

(frame_size가 담긴) nb_samples가 0인 경우 AVERROR(EINVAL)을 반환합니다. 그리고 av_frame_get_buffer의 경우에도 frame->nb_samples의 값이 0보다 크지 않으면 AVERROR(EINVAL)을 반환합니다.

// https://ffmpeg.org/doxygen/trunk/frame_8c_source.html#l00243
 int av_frame_get_buffer(AVFrame *frame, int align)
 {
     if (frame->format < 0)
         return AVERROR(EINVAL);
  
     if (frame->width > 0 && frame->height > 0)
         return get_video_buffer(frame, align);
     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
         return get_audio_buffer(frame, align);
  
     return AVERROR(EINVAL);
 }

이와 관련해 Q&A에 보면,

ffmpeg audio encoder frame size
; https://stackoverflow.com/questions/60997236/ffmpeg-audio-encoder-frame-size

코덱에 AV_CODEC_CAP_VARIABLE_FRAME_SIZE가 설정돼 있으면 avcodec_open2 호출에도 frame_size가 0으로 나온다고 합니다.

그런데, 제 경우에는 확인을 해봐도,

if ((codec->capabilities & ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE) == ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
{
    Console.WriteLine("Codec: ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE");
}

저 조건이 걸리지 않습니다. 왜냐하면, codec->capabilities 값이 0x00080022이고, AV_CODEC_CAP_VARIABLE_FRAME_SIZE는 0x00010000이기 때문입니다.




밑져야 본전이라는 생각으로 ^^ 일부러 (mp2에서 구했던) 1152를 c->frame_size에 대입해 봤습니다.

if (c->frame_size == 0)
{
    c->frame_size = 1152;
}

frame->nb_samples = c->frame_size;
frame->format = (int)c->sample_fmt;
frame->channel_layout = c->channel_layout;

/* allocate the data buffers */
ret = ffmpeg.av_frame_get_buffer(frame, 0);
if (ret < 0)
{
    Console.WriteLine("Could not allocate audio data buffers");
    break;
}

그랬더니 잘 동작합니다. ^^; 출력된 mp3 파일을 ffprobe로도 이렇게 확인할 수 있습니다.

C:\temp\output> ffprobe test.mp3
ffprobe version 4.4.1 Copyright (c) 2007-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[mp3 @ 000001EA6C3B0380] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'test.mp3':
  Duration: 00:00:05.27, start: 0.000000, bitrate: 96 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 96 kb/s

그런데 좀 이상하군요, 분명히 소스 코드에서는 "c->bit_rate = 64000;"라고 지정했는데 위의 출력에서는 96kb/s로 나옵니다. (MP2 인코딩 예제에서는 64kb/s로 나옵니다.) 이와 함께 Duration도 mp2 인코딩에서는 5.22가 나왔지만 위의 경우 5.27로 나옵니다.

한 가지 더 이상한 것은, MP2의 경우 "c = ffmpeg.avcodec_alloc_context3(codec);" 실행 시 c->bit_rate가 0으로 나오기 때문에 명시적으로 64000을 설정하는 것이 의미가 있지만, MP3의 경우에는 동일한 코드에서 c->bit_rate가 이미 128000으로 초기화가 되어 있습니다. 그걸 강제로 64000으로 설정하는 것이 의미가 없는 걸까요? (그렇다고 해도 상식적으로, 동일한 데이터에 bitrate가 높게 설정되면 시간이 줄어야 할 듯한데... ^^;)

참고로, 아래의 글을 보면,

BASIC4MCU | 하드웨어 | AUDIO | MPEG1 Audio Layer3 / mp3 파일구조
; https://www.basic4mcu.com/bbs/board.php?bo_table=gesiyo8&wr_id=480

"mp3는 프레임당 1152 비트가 할당되기 때문에 바이트 계산하면 144바이트가 된다."라는 설명이 나옵니다. 비디오의 경우에는 프레임이 하나의 영상 크기라는 것을 알겠지만, 오디오의 경우 frame_size가 명확히 다가오지 않습니다. 비디오 영상의 30fps라는 의미가 오디오에서는 보통 샘플링 주파수인 44100 Hz 등과 비교될 수 있을 것 같습니다. 그런 의미에서 어쩌면 오디오에서는 채널 수(스테레오 2채널)와 샘플링 비트(16비트 PCM)가 frame_size의 의미가 될 듯하지만, 1152와 32라는 숫자 간에 괴리감이 있습니다.

게다가 위의 설명에서 "프레임당 ... (크기)"에 대한 설명을 발췌하면 다음과 같이 서로 다른 의미를 갖는 동일한 설명이 됩니다.

mp3는 프레임당 1152 비트가 할당되기 때문에...[생략]... 프레임당 약 9558바이트의 Size가 된다.

음... 아무리 제가 이 분야로 초보라지만... ^^;




자, 어쨌든 우여곡절 끝에 인코딩을 성공했으니 그럼 지난번 예제에서 못했던 MP3 디코딩을 해볼까요? ^^

그 글에서도 언급했지만, 해당 예제를 AV_CODEC_ID_MP3 식별자로 바꿔 avcodec_find_decoder를 호출하면 이후 avcodec_send_packet에서 ret 값이 -1094995529로 오류를 반환합니다.

ret = ffmpeg.avcodec_send_packet(dec_ctx, packet);
if (ret < 0)
{
    Console.WriteLine($"Error submitting the packet to the decoder: {ret}");
    return false;
}

원래의 C 언어 예제에서는 위의 코드가 "return false"가 아닌 exit(1)로 되어 있기 때문에,

ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
    fprintf(stderr, "Error submitting the packet to the decoder\n");
    exit(1);
}

저도 C#으로 포팅하며 false를 반환한 경우 더 이상 디코딩 작업을 하지 않도록 변경한 것인데요. 재미있는 것은, 그냥 avcodec_send_packet에서 오류가 발생해도 그걸 무시하고 진행해도 된다는 점입니다. 즉, 그냥 true를 반환하도록 해서,

ret = ffmpeg.avcodec_send_packet(dec_ctx, packet);
if (ret < 0)
{
    Console.WriteLine($"Error submitting the packet to the decoder: {ret}");
    return true;
}

디코딩 작업을 계속 이어나가면 됩니다. 그럼 화면에는 다음과 같은 식으로 출력이 나오는데,

[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
Warning: the sample format the decoder produced is planar 0. This example will output the first channel only.
Play the output audio file with the command:
ffplay -f f32le -ac 2 -ar 44100 c:\temp\output\test_cs2.dat

3번의 "Header missing"이 나왔지만 그것에 상관없이 출력된 "test_cs2.dat" 파일은 정상적으로 ffplay로 재생이 됩니다.

대신 MP2 파일을 디코딩한 파일은 41,405KB 크기로 ffprobe의 결과가 adpcm_dtk였지만, MP3 파일의 경우에는 그 2배인 82,809KB에 ffprobe로는 결과가 나오지 않고,

C:\temp\output> ffprobe c:\temp\output\test_cs2.dat
ffprobe version 4.4.1 Copyright (c) 2007-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[luodat @ 0000026A8014D640] Format luodat detected only with low score of 1, misdetection possible!
Input #0, luodat, from 'c:\temp\output\test_cs2.dat':
  Duration: N/A, bitrate: N/A

대신 ffplay로 재생할 때 pcm_f32le라고 나옵니다.

C:\temp\output> ffplay -f f32le -ac 2 -ar 44100 c:\temp\output\test_cs2.dat
ffplay version 4.4.1 Copyright (c) 2003-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[f32le @ 000001FFFE6A5340] Estimating duration from bitrate, this may be inaccurate
Input #0, f32le, from 'c:\temp\output\test_cs2.dat':
  Duration: 00:04:00.35, bitrate: 2822 kb/s
  Stream #0:0: Audio: pcm_f32le, 44100 Hz, 2 channels, flt, 2822 kb/s
  49.46 M-A:  0.000 fd=   0 aq=  349KB vq=    0KB sq=    0B f=0/0

어쨌든, 뭔가 석연치 않은 점들이 있지만 일단 MP3 파일에 대한 디코딩/인코딩은 완료했습니다. ^^

(첨부 파일은 MP3 디코딩/인코딩 예제 코드를 포함합니다.)




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/25/2022]

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)
13818정성태11/15/20245263Windows: 272. Windows 11 24H2 - sudo 추가
13817정성태11/14/20244925Linux: 106. eBPF / bpf2go - (BPF_MAP_TYPE_HASH) Map을 이용한 전역 변수 구현
13816정성태11/14/20245386닷넷: 2312. C#, C++ - Windows / Linux 환경의 Thread Name 설정파일 다운로드1
13815정성태11/13/20244809Linux: 105. eBPF - bpf2go에서 전역 변수 설정 방법
13814정성태11/13/20245277닷넷: 2311. C# - Windows / Linux 환경에서 Native Thread ID 가져오기파일 다운로드1
13813정성태11/12/20245028닷넷: 2310. .NET의 Rune 타입과 emoji 표현파일 다운로드1
13812정성태11/11/20245248오류 유형: 933. Active Directory - The forest functional level is not supported.
13811정성태11/11/20244848Linux: 104. Linux - COLUMNS 환경변수가 언제나 80으로 설정되는 환경
13810정성태11/10/20245374Linux: 103. eBPF (bpf2go) - Tracepoint를 이용한 트레이스 (BPF_PROG_TYPE_TRACEPOINT)
13809정성태11/10/20245250Windows: 271. 윈도우 서버 2025 마이그레이션
13808정성태11/9/20245256오류 유형: 932. Linux - 커널 업그레이드 후 "error: bad shim signature" 오류 발생
13807정성태11/9/20244983Linux: 102. Linux - 커널 이미지 파일 서명 (Ubuntu 환경)
13806정성태11/8/20244901Windows: 270. 어댑터 상세 정보(Network Connection Details) 창의 내용이 비어 있는 경우
13805정성태11/8/20244736오류 유형: 931. Active Directory의 adprep 또는 복제가 안 되는 경우
13804정성태11/7/20245366Linux: 101. eBPF 함수의 인자를 다루는 방법
13803정성태11/7/20245319닷넷: 2309. C# - .NET Core에서 바뀐 DateTime.Ticks의 정밀도
13802정성태11/6/20245692Windows: 269. GetSystemTimeAsFileTime과 GetSystemTimePreciseAsFileTime의 차이점파일 다운로드1
13801정성태11/5/20245480Linux: 100. eBPF의 2가지 방식 - libbcc와 libbpf(CO-RE)
13800정성태11/3/20246321닷넷: 2308. C# - ICU 라이브러리를 활용한 문자열의 대소문자 변환 [2]파일 다운로드1
13799정성태11/2/20244906개발 환경 구성: 732. 모바일 웹 브라우저에서 유니코드 문자가 표시되지 않는 경우
13798정성태11/2/20245505개발 환경 구성: 731. 유니코드 - 출력 예시 및 폰트 찾기
13797정성태11/1/20245491C/C++: 185. C++ - 문자열의 대소문자를 변환하는 transform + std::tolower/toupper 방식의 문제점파일 다운로드1
13796정성태10/31/20245381C/C++: 184. C++ - ICU dll을 이용하는 예제 코드 (Windows)파일 다운로드1
13795정성태10/31/20245163Windows: 268. Windows - 리눅스 환경처럼 공백으로 끝나는 프롬프트 만들기
13794정성태10/30/20245261닷넷: 2307. C# - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
13793정성태10/28/20245134C/C++: 183. C++ - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...