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)
13247정성태2/7/20234962VS.NET IDE: 180. Visual Studio - 닷넷 소스 코드 디버깅 중 "Decompile source code"가 동작하는 않는 문제
13246정성태2/6/20234085개발 환경 구성: 664. Hyper-V에 설치한 리눅스 VM의 VHD 크기 늘리는 방법 - 두 번째 이야기
13245정성태2/6/20234627.NET Framework: 2093. C# - PEM 파일을 이용한 RSA 개인키/공개키 설정 방법파일 다운로드1
13244정성태2/5/20233983VS.NET IDE: 179. Visual Studio - External Tools에 Shell 내장 명령어 등록
13243정성태2/5/20234855디버깅 기술: 190. windbg - Win32 API 호출 시점에 BP 거는 방법 [1]
13242정성태2/4/20234292디버깅 기술: 189. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.UnauthorizedAccessException
13241정성태2/3/20233824디버깅 기술: 188. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.IO.FileNotFoundException
13240정성태2/1/20233983디버깅 기술: 187. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.Web.HttpException
13239정성태2/1/20233626디버깅 기술: 186. C# - CacheDependency의 숨겨진 예외 - System.Web.HttpException
13238정성태1/31/20235633.NET Framework: 2092. IIS 웹 사이트를 TLS 1.2 또는 TLS 1.3 프로토콜로만 운영하는 방법
13237정성태1/30/20235321.NET Framework: 2091. C# - 웹 사이트가 어떤 버전의 TLS/SSL을 지원하는지 확인하는 방법
13236정성태1/29/20234967개발 환경 구성: 663. openssl을 이용해 인트라넷 IIS 사이트의 SSL 인증서 생성
13235정성태1/29/20234508개발 환경 구성: 662. openssl - 윈도우 환경의 명령행에서 SAN 적용하는 방법
13234정성태1/28/20235550개발 환경 구성: 661. dnSpy를 이용해 소스 코드가 없는 .NET 어셈블리의 코드를 변경하는 방법 [1]
13233정성태1/28/20236897오류 유형: 840. C# - WebClient로 https 호출 시 "The request was aborted: Could not create SSL/TLS secure channel" 예외 발생
13232정성태1/27/20234698스크립트: 43. uwsgi의 --processes와 --threads 옵션
13231정성태1/27/20233615오류 유형: 839. python - TypeError: '...' object is not callable
13230정성태1/26/20234030개발 환경 구성: 660. WSL 2 내부로부터 호스트 측의 네트워크로 UDP 데이터가 1개의 패킷으로만 제한되는 문제
13229정성태1/25/20234968.NET Framework: 2090. C# - UDP Datagram의 최대 크기
13228정성태1/24/20235112.NET Framework: 2089. C# - WMI 논리 디스크가 속한 물리 디스크의 정보를 얻는 방법 [2]파일 다운로드1
13227정성태1/23/20234820개발 환경 구성: 659. Windows - IP MTU 값을 바꿀 수 있을까요? [1]
13226정성태1/23/20234493.NET Framework: 2088. .NET 5부터 지원하는 GetRawSocketOption 사용 시 주의할 점
13225정성태1/21/20233746개발 환경 구성: 658. Windows에서 실행 중인 소켓 서버를 다른 PC 또는 WSL에서 접속할 수 없는 경우
13224정성태1/21/20234090Windows: 221. Windows - Private/Public/Domain이 아닌 네트워크 어댑터 단위로 방화벽을 on/off하는 방법
13223정성태1/20/20234281오류 유형: 838. RDP 연결 오류 - The two computers couldn't connect in the amount of time allotted
13222정성태1/20/20233932개발 환경 구성: 657. WSL - DockerDesktop.vhdx 파일 위치를 옮기는 방법
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...