2

我正在使用 ffmpeg 进行视频转换以将视频转换为 h264 格式,但它需要 100% 的 CPU 和内存资源来影响服务器性能。下面是我转换视频的代码,请帮助我,但视频必须转换为 h264 格式。

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
4

2 回答 2

4

如果没有必要,最好的方法是避免转码。如果输入流已经使用具有良好编码参数的 H.264,只需复制编码 ( -c:v copy)。您可以使用 探测输入ffprobe

如果响应能力比编码性能更重要,并且您使用的是多核 CPU,则可以减少编码线程的数量。ffmpeg论据-threads n是。否则x264,在默认的基于帧的线程的情况下使用 1.5 x 核心线程。您还可以直接使用-x264opts(请参阅文档)设置 lib 选项。

于 2015-12-08T12:29:46.147 回答
1

除了@aergistal提到的内容之外,我还想指出一个-preset:v选项,它允许在压缩和 cpu 使用之间进行权衡。如果您想要快速编码并且您不太关心质量,请使用类似-preset:v veryfast,superfastultrafast. 请参阅wiki中的“2. 选择预设” 。

于 2015-12-08T16:49:33.613 回答