Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports Ffmpeg.Enums
- Imports Ffmpeg.Types.EventArgs
- #Region " Ffmpeg Task "
- Namespace Ffmpeg.Enums
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies a <c>FFMPEG.exe</c> task.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum FfmpegTask As Integer
- ''' <summary>
- ''' Converts an audio track to mp3.
- ''' </summary>
- ConvertAudioToMP3 = 0
- ''' <summary>
- ''' Checks the metadata of a video container.
- ''' </summary>
- CheckVideoMetadata = 1
- ''' <summary>
- ''' Removes the metadata from a video container.
- ''' </summary>
- RemoveVideoMetadata = 2
- ''' <summary>
- ''' Re-compresses the audio track of a video container.
- ''' </summary>
- RecompressAudio = 3
- ''' <summary>
- ''' Saves the audio track of a video container as Wav format.
- ''' </summary>
- SaveAudioAsWav = 4
- ''' <summary>
- ''' Demuxes a video track of a video container.
- ''' </summary>
- DemuxVideo = 5
- ''' <summary>
- ''' Demuxes an audio track of a video container.
- ''' </summary>
- DemuxAudio = 6
- End Enum
- End Namespace
- #End Region
- #Region " Ffmpeg Audio BitRate "
- Namespace Ffmpeg.Enums
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies a <c>FFMPEG.exe</c> audio bitrate.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum FfmpegAudioBitrate As Integer
- Kbps24 = 24
- Kbps32 = 32
- Kbps40 = 40
- Kbps48 = 48
- Kbps56 = 56
- Kbps64 = 64
- Kbps80 = 80
- Kbps96 = 96
- Kbps112 = 112
- Kbps128 = 128
- Kbps144 = 144
- Kbps160 = 160
- Kbps192 = 192
- Kbps224 = 224
- Kbps256 = 256
- Kbps320 = 320
- End Enum
- End Namespace
- #End Region
- #Region " Ffmpeg Audio Codec "
- Namespace Ffmpeg.Enums
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies a <c>FFMPEG.exe</c> audio codec name.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum FFMPEGAudioCodec As Integer
- ''' <summary>
- ''' LAME MP3.
- ''' </summary>
- libmp3lame
- ''' <summary>
- ''' Windows Media Audio.
- ''' </summary>
- wmav2
- End Enum
- End Namespace
- #End Region
- #Region " Ffmpeg Started EventArgs "
- Namespace Ffmpeg.Types.EventArgs
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Contains the event data of a <see cref="Converter.Started"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <ImmutableObject(True)>
- Public NotInheritable Class FfmpegStartedEventArgs
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="FfmpegStartedEventArgs"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="FfmpegStartedEventArgs"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="file">
- ''' The filepath that was passed as argument to the process.
- ''' </param>
- '''
- ''' <param name="task">
- ''' The <see cref="FfmpegTask"/> type that is being realized.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub New(ByVal file As String,
- ByVal task As FfmpegTask)
- Me.fileb = file
- Me.taskB = task
- End Sub
- #End Region
- #Region " Properties "
- ''' <summary>
- ''' Gets the filepath that was passed as argument to the process.
- ''' </summary>
- Public ReadOnly Property File As String
- Get
- Return Me.fileb
- End Get
- End Property
- Private ReadOnly fileb As String
- ''' <summary>
- ''' Gets the <see cref="FfmpegTask"/> task that is being realized.
- ''' </summary>
- Public ReadOnly Property Task As FfmpegTask
- Get
- Return Me.taskB
- End Get
- End Property
- Private ReadOnly taskB As FfmpegTask
- #End Region
- End Class
- End Namespace
- #End Region
- #Region " Ffmpeg Exited EventArgs "
- Namespace Ffmpeg.Types.EventArgs
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Contains the event data of a <see cref="Converter.Exited"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <ImmutableObject(True)>
- Public NotInheritable Class FfmpegExitedEventArgs
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="FfmpegExitedEventArgs"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="FfmpegExitedEventArgs"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="file">
- ''' The filepath that was passed as argument to the process.
- ''' </param>
- '''
- ''' <param name="task">
- ''' The <see cref="FfmpegTask"/> type that is being realized.
- ''' </param>
- '''
- ''' <param name="errors">
- ''' The error messages of the realized task (if any).
- ''' </param>
- '''
- ''' <param name="exitCode">
- ''' The process exit code.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub New(ByVal file As String,
- ByVal task As FfmpegTask,
- ByVal errors As List(Of String),
- ByVal exitCode As Integer)
- Me.fileb = file
- Me.taskB = task
- Me.errorsB = errors
- Me.exitCodeB = exitCode
- End Sub
- #End Region
- #Region " Properties "
- ''' <summary>
- ''' Gets the filepath that was passed as argument to the process.
- ''' </summary>
- Public ReadOnly Property File As String
- Get
- Return Me.fileb
- End Get
- End Property
- Private ReadOnly fileb As String
- ''' <summary>
- ''' Gets the <see cref="FfmpegTask"/> task that is being realized.
- ''' </summary>
- Public ReadOnly Property Task As FfmpegTask
- Get
- Return Me.taskB
- End Get
- End Property
- Private ReadOnly taskB As FfmpegTask
- ''' <summary>
- ''' Gets the error messages of the finished task (if any).
- ''' </summary>
- Public ReadOnly Property Errors As List(Of String)
- Get
- Return Me.errorsB
- End Get
- End Property
- Private ReadOnly errorsB As List(Of String)
- ''' <summary>
- ''' Gets the process exit code.
- ''' </summary>
- Public ReadOnly Property ExitCode As Integer
- Get
- Return Me.exitCodeB
- End Get
- End Property
- Private ReadOnly exitCodeB As Integer
- #End Region
- End Class
- End Namespace
- #End Region
- #Region " Ffmpeg Progress EventArgs "
- Namespace Ffmpeg.Types.EventArgs
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Contains the event data of a <see cref="Converter.ProgressChanged"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <ImmutableObject(True)>
- Public NotInheritable Class FfmpegProgressEventArgs
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="FfmpegProgressEventArgs"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="FfmpegProgressEventArgs"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="file">
- ''' The filepath that was passed as argument to the process.
- ''' </param>
- '''
- ''' <param name="task">
- ''' The <see cref="FfmpegTask"/> type that is being realized.
- ''' </param>
- '''
- ''' <param name="percent">
- ''' The <c>FFMPEG.exe</c> task percent done.
- ''' </param>
- '''
- ''' <param name="writtenBytes">
- ''' The total amount of written bytes.
- ''' </param>
- '''
- ''' <param name="videoDuration">
- ''' The input video duration.
- ''' </param>
- '''
- ''' <param name="time">
- ''' The processed video time.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub New(ByVal file As String,
- ByVal task As FfmpegTask,
- ByVal percent As Integer,
- ByVal writtenBytes As Double,
- ByVal videoDuration As TimeSpan,
- ByVal time As TimeSpan)
- Me.fileb = file
- Me.taskB = task
- Me.videoDurationB = videoDuration
- Me.timeB = time
- Me.writtenBytesB = writtenBytes
- Me.percentB = percent
- End Sub
- #End Region
- #Region " Properties "
- ''' <summary>
- ''' Gets the filepath that was passed as argument to the process.
- ''' </summary>
- Public ReadOnly Property File As String
- Get
- Return Me.fileb
- End Get
- End Property
- Private ReadOnly fileb As String
- ''' <summary>
- ''' The input Video Duration.
- ''' </summary>
- Public ReadOnly Property VideoDuration As TimeSpan
- Get
- Return Me.videoDurationB
- End Get
- End Property
- Private ReadOnly videoDurationB As TimeSpan
- ''' <summary>
- ''' The processed video time.
- ''' </summary>
- Public ReadOnly Property Time As TimeSpan
- Get
- Return Me.timeB
- End Get
- End Property
- Private ReadOnly timeB As TimeSpan
- ''' <summary>
- ''' The total amount of written bytes.
- ''' </summary>
- Public ReadOnly Property WrittenBytes As Double
- Get
- Return Me.writtenBytesB
- End Get
- End Property
- Private ReadOnly writtenBytesB As Double
- ''' <summary>
- ''' Gets the <see cref="FfmpegTask"/> task that is being realized.
- ''' </summary>
- Public ReadOnly Property Task As FfmpegTask
- Get
- Return Me.taskB
- End Get
- End Property
- Private ReadOnly taskB As FfmpegTask
- ''' <summary>
- ''' Gets the <c>FFMPEG.exe</c> task percent done.
- ''' </summary>
- Public ReadOnly Property Percent As Integer
- Get
- Return Me.percentB
- End Get
- End Property
- Private ReadOnly percentB As Integer
- #End Region
- End Class
- End Namespace
- #End Region
- #Region " Ffmpeg Converter "
- #Region " Public Members Summary "
- #Region " Constructors "
- ' New(String)
- #End Region
- #Region " Events "
- ' Started As EventHandler(Of FfmpegStartedEventArgs)
- ' Exited As EventHandler(Of FfmpegExitedEventArgs)
- ' ProgressChanged As EventHandler(Of FfmpegProgressEventArgs)
- #End Region
- #Region " Properties "
- ' FilePath As String
- ' Process As Process
- ' Exists As Boolean
- #End Region
- #Region " Functions "
- ' VideoContainsMetadata(String) As Boolean
- ' RemoveVideoMetadata(String, String, Boolean, Opt: Integer) As Integer
- ' RecompressAudio(String, String, Boolean, FfmpegAudioCodec, FfmpegAudioBitrate, Opt: Boolean, Opt: Integer) As Integer
- ' SaveAudioAsWav(String, String, Boolean, Opt: Integer) As Integer
- ' DemuxAudio(String, String, Boolean, Opt: Integer) As Integer
- ' DemuxVideo(String, String, Boolean, Opt: Integer) As Integer
- #End Region
- #End Region
- #Region " CommandLine Parameter legend "
- '-y | Overwrite output files without asking.
- '-n | Do not overwrite output files, and exit immediately if a specified output file already exists.
- '-threads | Specify the cpu threads to use.
- '-nostdin | Disable interaction on standard input.
- '-vcodec | Set the video codec.
- '-acodec | Set the audio codec.
- '-vn | Disable video recording.
- '-an | Disable audio recording.
- ' -c copy -map_metadata -1
- ' Don't add metadata.
- #End Region
- #Region " Usage Examples "
- 'Private WithEvents ff As New Converter(".\FFMPEG.exe")
- '
- 'Private Shadows Sub Shown() Handles MyBase.Shown
- '
- ' ' Checks if FFMPEG executable is available.
- ' MsgBox(ff.Exists)
- '
- ' ' Checks if a video has metadata
- ' MsgBox(ff.VideoContainsMetadata("C:\Video.mkv"))
- '
- ' ' Remove metadata from video
- ' ff.RemoveVideoMetadata("C:\Input.mkv", "C:\Output.mkv", True, 4)
- '
- ' ' reCompress the audio track of a video
- ' ff.RecompressAudio("C:\Input.mkv", "C:\Output.mkv", True, FfmpegAudioCodec.libmp3lame, FfmpegAudioBitrate.Kbps128, True, 4)
- '
- ' ' Demuxes the audio track of file
- ' ff.DemuxAudio("C:\Input.wmv", "C:\Output.wma", True, 4)
- '
- ' ' Demuxes the video track of file
- ' ff.DemuxVideo("C:\Input.mp4", "C:\Output.h264", True, 4)
- '
- ' ' Demuxes the video track of file
- ' ff.SaveAudioAsWav("C:\Input.mkv", "C:\Output.wav", True, 4)
- '
- 'End Sub
- '
- 'Private Sub Ffmeg_Started(ByVal sender As Object, ByVal e As FfmpegStartedEventArgs) _
- 'Handles ff.Started
- '
- ' ProgressBar1.Value = ProgressBar1.Minimum
- '
- ' Dim sb As New System.Text.StringBuilder
- '
- ' sb.AppendLine(String.Format("Started an ""{0}"" operation", e.Task.ToString()))
- ' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ' sb.AppendLine(String.Format("FFMPEG process PID is: ""{0}""", DirectCast(sender, Converter).Process.Id))
- '
- ' MessageBox.Show(sb.ToString(), "FFMPEG", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '
- 'End Sub
- '
- 'Private Sub Ffmeg_Exited(ByVal sender As Object, ByVal e As FfmpegExitedEventArgs) _
- 'Handles ff.Exited
- '
- ' Dim sb As New System.Text.StringBuilder
- '
- ' sb.AppendLine(String.Format("Finished an ""{0}"" operation", e.Task.ToString()))
- ' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ' sb.AppendLine(String.Format("FFMPEG process PID is: {0}", DirectCast(sender, Process).Id))
- '
- ' If e.Errors.Count <> 0 Then
- ' sb.AppendLine(String.Format("Errors during operation: {0}", String.Join(Environment.NewLine, e.Errors)))
- ' End If
- '
- ' MessageBox.Show(sb.ToString(), "FFMPEG", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '
- 'End Sub
- '
- 'Private Sub Ffmeg_ProgressChanged(ByVal sender As Object, e As FfmpegProgressEventArgs) _
- 'Handles ff.ProgressChanged
- '
- ' ProgressBar1.Value = e.Percent
- '
- ' Label1.Text = "Percent Done: " & CStr(e.Percent) & "%"
- ' Label2.Text = "Video Duration: " & e.VideoDuration.ToString("hh\:mm\:ss")
- ' Label3.Text = "Written Duration: " & e.Time.ToString("hh\:mm\:ss")
- ' Label4.Text = "Written Data: " & (e.WrittenBytes / 1024L * 1024L).ToString("n1") & "MB"
- '
- 'End Sub
- #End Region
- Namespace Ffmpeg.Types
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A wrapper of <c>FFMPEG.exe</c> application.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Private WithEvents ff As New Converter(".\FFMPEG.exe")
- '''
- ''' Private Shadows Sub Shown() Handles MyBase.Shown
- '''
- ''' ' Checks if FFMPEG executable is available.
- ''' MsgBox(ff.Exists)
- '''
- ''' ' Checks if a video has metadata
- ''' MsgBox(ff.VideoContainsMetadata("C:\Video.mkv"))
- '''
- ''' ' Remove metadata from video
- ''' ff.RemoveVideoMetadata("C:\Input.mkv", "C:\Output.mkv", True, 4)
- '''
- ''' ' reCompress the audio track of a video
- ''' ff.RecompressAudio("C:\Input.mkv", "C:\Output.mkv", True, FfmpegAudioCodec.libmp3lame, FfmpegAudioBitrate.Kbps128, True, 4)
- '''
- ''' ' Demuxes the audio track of file
- ''' ff.DemuxAudio("C:\Input.wmv", "C:\Output.wma", True, 4)
- '''
- ''' ' Demuxes the video track of file
- ''' ff.DemuxVideo("C:\Input.mp4", "C:\Output.h264", True, 4)
- '''
- ''' ' Demuxes the video track of file
- ''' ff.SaveAudioAsWav("C:\Input.mkv", "C:\Output.wav", True, 4)
- '''
- ''' End Sub
- '''
- ''' Private Sub Ffmeg_Started(ByVal sender As Object, ByVal e As FfmpegStartedEventArgs) _
- ''' Handles ff.Started
- '''
- ''' ProgressBar1.Value = ProgressBar1.Minimum
- '''
- ''' Dim sb As New System.Text.StringBuilder
- '''
- ''' sb.AppendLine(String.Format("Started an ""{0}"" operation", e.Task.ToString()))
- ''' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ''' sb.AppendLine(String.Format("FFMPEG process PID is: ""{0}""", DirectCast(sender, Converter).Process.Id))
- '''
- ''' MessageBox.Show(sb.ToString(), "FFMPEG", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '''
- ''' End Sub
- '''
- ''' Private Sub Ffmeg_Exited(ByVal sender As Object, ByVal e As FfmpegExitedEventArgs) _
- ''' Handles ff.Exited
- '''
- ''' Dim sb As New System.Text.StringBuilder
- '''
- ''' sb.AppendLine(String.Format("Finished an ""{0}"" operation", e.Task.ToString()))
- ''' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ''' sb.AppendLine(String.Format("FFMPEG process PID is: {0}", DirectCast(sender, Process).Id))
- '''
- ''' If e.Errors.Count <> 0 Then
- ''' sb.AppendLine(String.Format("Errors during operation: {0}", String.Join(Environment.NewLine, e.Errors)))
- ''' End If
- '''
- ''' MessageBox.Show(sb.ToString(), "FFMPEG", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '''
- ''' End Sub
- '''
- ''' Private Sub Ffmeg_ProgressChanged(ByVal sender As Object, e As FfmpegProgressEventArgs) _
- ''' Handles ff.ProgressChanged
- '''
- ''' ProgressBar1.Value = e.Percent
- '''
- ''' Label1.Text = "Percent Done: " & CStr(e.Percent) & "%"
- ''' Label2.Text = "Video Duration: " & e.VideoDuration.ToString("hh\:mm\:ss")
- ''' Label3.Text = "Written Duration: " & e.Time.ToString("hh\:mm\:ss")
- ''' Label4.Text = "Written Data: " & (e.WrittenBytes / 1024L * 1024L).ToString("n1") & "MB"
- '''
- ''' End Sub
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- <ImmutableObject(True)>
- Public NotInheritable Class Converter : Implements IDisposable
- #Region " Private Fields "
- ''' <summary>
- ''' The <c>FFMPEG.exe</c> <see cref="Process"/> instance
- ''' </summary>
- Private ReadOnly processB As Process
- #End Region
- #Region " Events "
- ''' <summary>
- ''' Event raised when the <c>FFMPEG.exe</c> process has been started.
- ''' </summary>
- Public Event Started As EventHandler(Of FfmpegStartedEventArgs)
- ''' <summary>
- ''' Event raised when the <c>FFMPEG.exe</c> process has exited.
- ''' </summary>
- Public Event Exited As EventHandler(Of FfmpegExitedEventArgs)
- ''' <summary>
- ''' Event raised when <c>FFMPEG.exe</c> task progress has been changed.
- ''' </summary>
- Public Event ProgressChanged As EventHandler(Of FfmpegProgressEventArgs)
- #End Region
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the <c>FFMPEG.exe</c> filepath.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property FilePath As String
- Get
- Return Me.filepathB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing field )
- ''' The <c>FFMPEG.exe</c> filepath.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private filepathB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets a value indicating whether the <c>FFMPEG.exe</c> file Exists.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property Exists As Boolean
- Get
- Return File.Exists(Me.FilePath)
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the <c>FFMPEG.exe</c> <see cref="Process"/> instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property Process As Process
- Get
- Return Me.processB
- End Get
- End Property
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="Converter"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="Converter" /> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="filepath">
- ''' The <c>FFMPEG.exe</c> filepath.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub New(ByVal filepath As String)
- Me.filepathB = filepath
- Me.processB =
- New Process With {.StartInfo =
- New ProcessStartInfo With {
- .FileName = filepath,
- .CreateNoWindow = True,
- .UseShellExecute = False,
- .RedirectStandardError = True,
- .RedirectStandardOutput = True,
- .StandardErrorEncoding = Encoding.Default,
- .StandardOutputEncoding = Encoding.Default
- }
- }
- End Sub
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether a video file contains metadata fields.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="videoFilepath">
- ''' The source video filepath.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if video file contains metadata fields, otherwise, <see langword="False"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function VideoContainsMetadata(ByVal videoFilepath As String) As Boolean
- ' Unique temp file to write the FFMPEG.exe output.
- Dim tempFile As String = Path.GetTempFileName()
- Me.processB.StartInfo.Arguments =
- String.Format("-y -i ""{0}"" -f ffmetadata ""{1}""",
- videoFilepath, tempFile)
- Dim exitCode As Integer = Me.Run(Me.processB, videoFilepath, FfmpegTask.CheckVideoMetadata)
- Return File.ReadAllText(tempFile).Replace(";FFMETADATA1", "").Trim.Length <> 0
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes the metadata tags from a video file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="inputFilepath">
- ''' The source video filepath.
- ''' </param>
- '''
- ''' <param name="outputFilepath">
- ''' The target video filepath.
- ''' </param>
- '''
- ''' <param name="overWrite">
- ''' A value indicating whether to overwrite the target filepath if already exists.
- ''' </param>
- '''
- ''' <param name="threads">
- ''' The amount of threads to use.
- ''' <para></para>
- ''' Default value is <c>1</c>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The process exit code.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function RemoveVideoMetadata(ByVal inputFilepath As String,
- ByVal outputFilepath As String,
- ByVal overWrite As Boolean,
- Optional ByVal threads As Integer = 1) As Integer
- Me.processB.StartInfo.Arguments =
- String.Format("-nostdin -threads {0} {1} -i ""{2}"" -c copy -map_metadata -1 ""{3}""",
- threads,
- If(overWrite, "-y", "-n"),
- inputFilepath,
- outputFilepath)
- Return Me.Run(Me.processB, inputFilepath, FfmpegTask.RemoveVideoMetadata)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ReCompress the audio track of a video file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="inputFilepath">
- ''' The source video filepath.
- ''' </param>
- '''
- ''' <param name="outputFilepath">
- ''' The target video filepath.
- ''' </param>
- '''
- ''' <param name="overWrite">
- ''' A value indicating whether to overwrite the target filepath if already exists.
- ''' </param>
- '''
- ''' <param name="audioCodec">
- ''' The audio codec to use.
- ''' </param>
- '''
- ''' <param name="bitrate">
- ''' The audio bitrate.
- ''' </param>
- '''
- ''' <param name="preserveMetadata">
- ''' A value indicating whether to preserve the source video metadata in the target video.
- ''' </param>
- '''
- ''' <param name="threads">
- ''' The amount of threads to use.
- ''' <para></para>
- ''' Default value is <c>1</c>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The process exit code.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function RecompressAudio(ByVal inputFilepath As String,
- ByVal outputFilepath As String,
- ByVal overWrite As Boolean,
- ByVal audioCodec As FFMPEGAudioCodec,
- ByVal bitrate As FfmpegAudioBitrate,
- Optional ByVal preserveMetadata As Boolean = False,
- Optional ByVal threads As Integer = 1) As Integer
- Me.processB.StartInfo.Arguments =
- String.Format("-nostdin -threads {0} {1} -i ""{2}"" {3} -vcodec copy -acodec {4} -ab {5} ""{6}""",
- threads,
- If(overWrite, "-y", "-n"),
- inputFilepath,
- If(preserveMetadata, "", "-c copy -map_metadata -1"),
- audioCodec.ToString(),
- CStr(bitrate) & "k",
- outputFilepath)
- Return Me.Run(Me.processB, inputFilepath, FfmpegTask.RecompressAudio)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Demuxes the audio track of a video file to save it as WAV format.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="inputFilepath">
- ''' The source video filepath.
- ''' </param>
- '''
- ''' <param name="outputFilepath">
- ''' The target audio filepath.
- ''' </param>
- '''
- ''' <param name="overWrite">
- ''' A value indicating whether to overwrite the target filepath if already exists.
- ''' </param>
- '''
- ''' <param name="threads">
- ''' The amount of threads to use.
- ''' <para></para>
- ''' Default value is <c>1</c>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The process exit code.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function SaveAudioAsWav(ByVal inputFilepath As String,
- ByVal outputFilepath As String,
- ByVal overWrite As Boolean,
- Optional ByVal threads As Integer = 1) As Integer
- Me.processB.StartInfo.Arguments =
- String.Format("-nostdin -threads {0} {1} -i ""{2}"" -acodec pcm_s16le -ac 1 -vn ""{3}""",
- threads,
- If(overWrite, "-y", "-n"),
- inputFilepath,
- outputFilepath)
- Return Me.Run(Me.processB, inputFilepath, FfmpegTask.SaveAudioAsWav)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Demuxes the video track of a video file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="inputFilepath">
- ''' The source video filepath.
- ''' </param>
- '''
- ''' <param name="outputFilepath">
- ''' The target video filepath.
- ''' </param>
- '''
- ''' <param name="overWrite">
- ''' A value indicating whether to overwrite the target filepath if already exists.
- ''' </param>
- '''
- ''' <param name="threads">
- ''' The amount of threads to use.
- ''' <para></para>
- ''' Default value is <c>1</c>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The process exit code.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function DemuxVideo(ByVal inputFilepath As String,
- ByVal outputFilepath As String,
- ByVal overWrite As Boolean,
- Optional ByVal threads As Integer = 1) As Integer
- Me.processB.StartInfo.Arguments =
- String.Format("-nostdin -threads {0} {1} -i ""{2}"" -vcodec copy -an ""{3}""",
- threads,
- If(overWrite, "-y", "-n"),
- inputFilepath,
- outputFilepath)
- Return Me.Run(Me.processB, inputFilepath, FfmpegTask.DemuxVideo)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Demuxes the audio track of a video file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="inputFilepath">
- ''' The source video filepath.
- ''' </param>
- '''
- ''' <param name="outputFilepath">
- ''' The target audio filepath.
- ''' </param>
- '''
- ''' <param name="overWrite">
- ''' A value indicating whether to overwrite the target filepath if already exists.
- ''' </param>
- '''
- ''' <param name="threads">
- ''' The amount of threads to use.
- ''' <para></para>
- ''' Default value is <c>1</c>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The process exit code.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- Public Function DemuxAudio(ByVal inputFilepath As String,
- ByVal outputFilepath As String,
- ByVal overWrite As Boolean,
- Optional ByVal threads As Integer = 1) As Integer
- Me.processB.StartInfo.Arguments =
- String.Format("-nostdin -threads {0} {1} -i ""{2}"" -acodec copy -vn ""{3}""",
- threads,
- If(overWrite, "-y", "-n"),
- inputFilepath,
- outputFilepath)
- Return Me.Run(Me.processB, inputFilepath, FfmpegTask.DemuxAudio)
- End Function
- #End Region
- #Region " Private Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Runs a specific task of <c>FFMPEG.exe</c>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Private Function Run(ByVal p As Process,
- ByVal file As String,
- ByVal task As FfmpegTask) As Integer
- Dim outputLine As String
- Dim videoDuration As TimeSpan
- Dim time As TimeSpan
- Dim errors As New List(Of String)
- Dim exitCode As Integer
- p.Start()
- If Me.StartedEvent IsNot Nothing Then
- Dim startedArgs As New FfmpegStartedEventArgs(file, task)
- RaiseEvent Started(Me, startedArgs)
- End If
- If Me.ProgressChangedEvent IsNot Nothing Then
- While Not p.StandardError.EndOfStream
- ' Parse the Input Video Duration to calculate the percentage.
- Do Until (videoDuration.TotalMilliseconds > 0)
- outputLine = p.StandardError.ReadLine().ToLower()
- If outputLine.Contains("duration") Then
- Try
- videoDuration = TimeSpan.Parse(outputLine.Replace("duration:", "").
- Split(","c).FirstOrDefault)
- Catch ex As FormatException
- videoDuration = TimeSpan.Parse("24:00:00") ' 00:00:00
- End Try
- End If
- Loop
- ' Parse the percentage and other values.
- outputLine = p.StandardError.ReadLine().ToLower()
- If outputLine.StartsWith("frame=") Then
- time = TimeSpan.Parse(outputLine.Split("="c)(5).Split.First())
- Dim progressArgs As New FfmpegProgressEventArgs(
- file,
- task,
- CInt((time.TotalSeconds / videoDuration.TotalSeconds) * 100),
- CDbl(outputLine.Split("="c)(4).Trim.Split.First.Replace("kb", "")) / 1024,
- videoDuration,
- time)
- RaiseEvent ProgressChanged(Me, progressArgs)
- ElseIf (outputLine.Contains("error") OrElse outputLine.Contains("warning")) Then
- errors.Add(outputLine)
- #If DEBUG Then
- Debug.WriteLine("[DEBUG] FFMPEG Error: " & outputLine)
- #End If
- End If
- End While
- Else
- p.WaitForExit()
- End If
- exitCode = p.ExitCode
- If Me.ExitedEvent IsNot Nothing Then
- Dim exitedArgs As New FfmpegExitedEventArgs(file, task, errors, exitCode)
- RaiseEvent Exited(Me, exitedArgs)
- End If
- ' FFMPEG.Close()
- Return exitCode
- End Function
- #End Region
- #Region " IDisposable Implementation "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Flag to detect redundant calls when disposing.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private isDisposed As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Releases all the resources used by this instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub Dispose() Implements IDisposable.Dispose
- Me.Dispose(isDisposing:=True)
- GC.SuppressFinalize(obj:=Me)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="isDisposing">
- ''' <see langword="True"/> to release both managed and unmanaged resources;
- ''' <see langword="False"/> to release only unmanaged resources.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Private Sub Dispose(ByVal isDisposing As Boolean)
- If (Not Me.isDisposed) AndAlso (isDisposing) Then
- If (Me.processB IsNot Nothing) Then
- 'Try
- ' Me.processB.Kill()
- '
- 'Catch ex As Exception
- '
- 'End Try
- Me.processB.Dispose()
- Me.filepathB = ""
- End If
- End If
- Me.isDisposed = True
- End Sub
- #End Region
- End Class
- End Namespace
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment