Need example for recording STEREO-MIX + MIC

Started by G.,

G.

       Dim ffmpegArguments As String = "-fflags +genpts+igndts -f gdigrab -framerate 25 -video_size {2}x{3} -offset_x {0} -offset_y {1} -i desktop -f s16le -ar 44100 -ac 2 -i - " &
                                       "-vf ""drawtext=fontfile='C\:/Windows/Fonts/arial.ttf':text='%{{localtime\:%Y-%m-%d}} %{{localtime\:%X}}':x=10:y=10:fontsize=20:" &
                                       "fontcolor=white:box=1:boxcolor=black@0.8:boxborderw=5"" -map 0:v -map 1:a -c:v libx264 -preset ultrafast -crf 22 -pix_fmt yuv420p -r 25 " &
                                       "-c:a aac ""c:\prova2\fftest.mkv"" -y"

       ffmpegArguments = String.Format(ffmpegArguments, Screen.PrimaryScreen.WorkingArea.X, Screen.PrimaryScreen.WorkingArea.Y, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)

       Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)

       Dim mixer As Integer = AddOn.Mix.BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_MIXER_NONSTOP Or BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)
       Dim encoder = BassEnc.BASS_Encode_Start(mixer, "C:\Program Files\ffmpeg\bin\ffmpeg " & ffmpegArguments, BASSEncode.BASS_ENCODE_AUTOFREE, Nothing, 0)

       Dim micHandle As Integer
       For d As Integer = 0 To Bass.BASS_RecordGetDeviceInfos.Count - 1
           With Bass.BASS_RecordGetDeviceInfos(d)
               If .id IsNot Nothing AndAlso .status.HasFlag(BASSDeviceInfo.BASS_DEVICE_ENABLED) AndAlso .status.HasFlag(BASSDeviceInfo.BASS_DEVICE_DEFAULTCOM) Then
                   Bass.BASS_RecordInit(d) : Bass.BASS_RecordSetDevice(d)
                   micHandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, BASSRecordProc.RECORDPROC_NONE, IntPtr.Zero)
                   AddOn.Mix.BassMix.BASS_Mixer_StreamAddChannel(mixer, micHandle, BASSFlag.BASS_STREAM_AUTOFREE)
                   Exit For
               End If
           End With
       Next d

       Dim listOfLoopbackDevice As New List(Of Integer)
       For d As Integer = 0 To Bass.BASS_RecordGetDeviceInfos.Count - 1
           With Bass.BASS_RecordGetDeviceInfos(d)
               If .id IsNot Nothing AndAlso .status.HasFlag(BASSDeviceInfo.BASS_DEVICE_ENABLED) AndAlso .status.HasFlag(BASSDeviceInfo.BASS_DEVICE_LOOPBACK) Then
                   Bass.BASS_RecordInit(d) : Bass.BASS_RecordSetDevice(d)
                   listOfLoopbackDevice.Add(Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, BASSRecordProc.RECORDPROC_NONE, IntPtr.Zero))
                   AddOn.Mix.BassMix.BASS_Mixer_StreamAddChannel(mixer, listOfLoopbackDevice.Last, BASSFlag.BASS_STREAM_AUTOFREE)
               End If
           End With
       Next d

       stopMe = False
       Bass.BASS_ChannelPlay(micHandle, False)
       listOfLoopbackDevice.ForEach(Sub(T) Bass.BASS_ChannelPlay(T, False))

       Do Until stopMe
           Dim buffer(50000) As Byte
           Dim avail As Integer = Bass.BASS_ChannelGetData(micHandle, 0, BASSData.BASS_DATA_AVAILABLE)
           Dim x As Integer = Bass.BASS_ChannelGetData(mixer, buffer, Math.Min(avail, buffer.Length))
           Application.DoEvents()
           Threading.Thread.Sleep(100)
       Loop

       Bass.BASS_ChannelStop(micHandle)
       listOfLoopbackDevice.ForEach(Sub(T) Bass.BASS_ChannelStop(T))
       Bass.BASS_ChannelFree(mixer)
       MessageBox.Show("Finish!!")

OK... updates....

I managed to get the combined video and audio recording to work... the code above is the current one...
The only thing is that instead of hearing the actual audio, I only hear a very loud noise/hiss... I think it has something to do with the encoding itself...

Ian @ un4seen

It looks like you're telling ffmpeg to expect raw (headerless) data with "-f s16le -ar 44100 -ac 2" in the command-line. Instead of doing that, it's usually better to have BASS_Encode_Start pass the sample format to the encoder via a WAVE header, which is what it does by default. But if you would prefer to use raw data then you can add the BASS_ENCODE_NOHEAD flag to the BASS_Encode_Start call. Please see the BASS_Encode_Start documentation for details.