<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<div class="moz-cite-prefix">El 23/10/18 a las 12:59, Matthieu
Regnauld escribió:<br>
</div>
<blockquote type="cite"
cite="mid:CADjM6tCMDHBV1bs+dxq_Sqh6M=+79R-tUdt0S6JgNqhHEG+BfQ@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">Thank you for your help.
<div><br>
</div>
<div>That said, I'm new to FFMpeg, and I still struggle.</div>
<div>Could you tell me what I have to fix in my code to have a
clear sound and also, if possible, how to resample it (from
44100 Hz to 48000 Hz, for example)?</div>
<div><br>
</div>
<div>Here is my code: <a
href="https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a"
target="_blank" moz-do-not-send="true">https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a</a></div>
<div><br>
</div>
<div>Thank you.</div>
</div>
</blockquote>
<p>First, you need to set swrContext to NULL at the beginning, like:</p>
<p><span style="color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">AVFrame *frame = </span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">NULL</span><span style="color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">;
</span><span style="color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">SwrContext *swrContext = NULL;</span></p>
<p>Then, here you should set the output frequency:</p>
<p><tt>int out_sample_rate = 48000;</tt><tt><br>
swr_alloc_set_opts(swrContext, AV_CH_LAYOUT_STEREO,
AV_SAMPLE_FMT_FLT, out_sample_rate,</tt><tt><br>
</tt><tt> codecContext->channel_layout,
codecContext->sample_fmt, codecContext->sample_rate, 0,</tt><tt><br>
</tt><tt> NULL);</tt><tt><br>
</tt><br>
</p>
<p>Then, you should not need to memcpy anything as that's what
swr_convert should do for you. However, you might want to set
your buffer bigger than two channels as avcodec_receive_frame
might return multiple frames of sound. By doing that, you won't
have to worry about overrunning the buffer. Also, you might want
to use extended_data, for formats that have more than 4 channels.<br>
</p>
<p>// Somewhere else<br>
localBuffer = av_malloc( sizeof(float) * out_sample_rate *
nb_samples + padding);<br>
</p>
<p>//<br>
</p>
<p>swr_convert(swrContext, (uint8_t**)&localBuffer,
frame->nb_samples, (const uint8_t **) frame->extended_data,
frame->nb_samples);<br>
<br>
<span style="color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"></span></p>
<pre class="moz-signature" cols="72">--
Gonzalo Garramuño</pre>
</body>
</html>