How to minimize usage of CPU/memory by ffmpeg when converting video mp4 h264

Programmer picture Programmer · Dec 8, 2015 · Viewed 8k times · Source

I am using ffmpeg for video conversion to convert video in h264 format but it takes 100% CPU and memory resources that effect the server performance. below is my code that convert the video please help me on this but video must convert in h264 format.

Public Function ConvertVideo() As Boolean
    Dim path = CurrentVideoPath
    Dim fileName = CurrentVideoName
    Dim retVal As Boolean = False
    Try
        'Form1.tmrConversion.Stop()
        '------------------
        ' fileName = IO.Path.GetFileNameWithoutExtension(fileName) + ".mp4"
        _mHandler = New MediaHandler()
        '------------------
        If (File.Exists(SourcePath + path + fileName)) Then
            newFileName = DateTime.Now.ToFileTime().ToString()
            'videoClassObj.UpdateStatus("Converting")
            '------------------
            'MessageBox.Show(DestinationPath + path + fileName)
            'videoClassObj.UpdateStatus("Done")
            ' Return True
            '------------------
            _mHandler.FFMPEGPath = Application.StartupPath & "\ffmpeg\bin\ffmpeg.exe"
            _mHandler.InputPath = SourcePath + path
            _mHandler.OutputPath = DestinationPath + path
            If (Not Directory.Exists(DestinationPath + path)) Then
                Directory.CreateDirectory(DestinationPath + path)
            End If
            _mHandler.FileName = fileName
            _mHandler.OutputFileName = newFileName
            _mHandler.Parameters = " -fpre """ & Application.StartupPath & "\\ffmpeg\presets\\libx264-ipod640.ffpreset" & """"
            _mHandler.BackgroundProcessing = True
            Dim extension = IO.Path.GetExtension(fileName)

            _mHandler.BackgroundProcessing = True
            _mHandler.VCodec = "libx264"
            Dim infoupload As VideoInfo = _mHandler.Get_Info()
            _mHandler.OutputExtension = ".mp4"
            If String.IsNullOrEmpty(infoupload.SamplingRate.Trim) Then
                _mHandler.Audio_SamplingRate = 44100
            Else
                _mHandler.Audio_SamplingRate = infoupload.SamplingRate.Trim().Split(" ")(0)
            End If


            _mHandler.Height = infoupload.Height
            _mHandler.Width = infoupload.Width
            'if (not extension.tolower().equals(".avi")) then
            '    _mhandler.video_bitrate = infoupload.video_bitrate.trim().split(" ")(0)
            'end if
            If String.IsNullOrEmpty(infoupload.Audio_Bitrate.Trim) Then
                _mHandler.Audio_Bitrate = 128
            Else
                _mHandler.Audio_Bitrate = infoupload.Audio_Bitrate.Trim().Split(" ")(0)
            End If


            Dim info As VideoInfo = _mHandler.ProcessMedia()

            If (info.ErrorCode = 0) Then
                While (_mHandler.vinfo.ProcessingCompleted < 100)
                    Console.WriteLine("Converting............")

                End While

                Dim Name = IO.Path.GetFileNameWithoutExtension(CurrentVideoName)
                Name = Name & ".mp4"

                Dim topath = DestinationPath + CurrentVideoPath
                Dim ffmpegpath As String = Application.StartupPath & "\ffmpeg\bin\ffmpeg.exe"
                Dim presetPath As String = Application.StartupPath & "\\ffmpeg\presets\\libx264-ipod640.ffpreset"

                Dim resolution As String = CompressVideosToHD(newFileName + ".mp4", topath)
                MoveHDfile(topath + IO.Path.GetFileNameWithoutExtension(newFileName))



                retVal = True

            Else
                retVal = False
            End If

        End If
    Catch ex As Exception
        'writeErrorLog(ex, "", "ConvertVideo", "")
        'frmConversion.UpdateStatus("Stopped")
        retVal = False
    Finally
        'Form1.tmrConversion.Start()
    End Try

    Return retVal
End Function

Answer

aergistal picture aergistal · Dec 8, 2015

The best way is to avoid transcoding if it's not necessary. If the input stream already uses H.264 with the good encoding parameters just copy the encoding (-c:v copy). You can probe the input with ffprobe.

If responsiveness is more important than encoding performance and you're using a multi-core CPU you can lower the number of encoding threads. The ffmpeg argument is -threads n. Otherwise x264 uses 1.5 x cores threads in the case of frame-based threading which is the default. You can also set the lib options directly with -x264opts (see docs).