我正在使用 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