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 コアのスレッドを使用します。lib オプションを直接設定することもできます-x264opts( docsを参照)。

于 2015-12-08T12:29:46.147 に答える
1

@aergistalが言及したことに加えて-preset:v、圧縮と CPU 使用量のトレードオフを設定できるオプションについても指摘したいと思います。高速なエンコードが必要で、品質をそれほど気にしない場合は-preset:v veryfast、 、superfastまたはultrafast. wikiの「2. プリセットを選択する」を参照してください。

于 2015-12-08T16:49:33.613 に答える