Share via


VB.Net - Animations and Movies


Overview


This is a small application that creates an animation in AVI or GIF format from user selected images. This functionality isn't possible in native VB.Net, but you can run the freeware program FFmpeg with the process class which produces video and GIF output from a contiguous sequence of numbered images.

The application is made up of the GUI code that handles all of the GUI related events such as adding, organizing, and removing image files, enabling or disabling buttons, etc...

The imageUtilities class contains three static methods, one private and only used internally within the class. The two public methods are createGIF and createAVI.


Figure 1. The GUI


The imageUtilities class


The image files chosen to be used in the animation or movie are copied locally and renamed in a sequentially numbered format that works with FFmpeg. This allows up to 100,000 frames to be used. The original image files are left intact in their original location.

Public Class  imageUtilities
 
    ''' <summary>
    ''' Makes local copies of image files
    ''' first deleting any existing local image files
    ''' </summary>
    ''' <param name="files">an array of IO.FileInfo to be used in animation</param>
    Private Shared  Sub copyFiles(files() As IO.FileInfo)
        Dim oldFiles() As String  = IO.Directory.GetFiles("images")
        For x As Integer  = 0 To  oldFiles.GetUpperBound(0)
            IO.File.Delete(oldFiles(x))
        Next
        For x As Integer  = 0 To  files.GetUpperBound(0)
            files(x).CopyTo($"images\frame{x.ToString("00000")}.png")
        Next
    End Sub
 
    ''' <summary>
    ''' Creates an animated GIF using images, framerate and output filename specified
    ''' </summary>
    ''' <param name="files">an array of IO.FileInfo to be used in animation</param>
    ''' <param name="outputFilename">where to save the GIF</param>
    ''' <param name="fps">the framerate</param>
    Public Shared  Sub createGIF(files() As IO.FileInfo, outputFilename As String, fps As  Integer)
        'gif
        copyFiles(files)
        Dim tempFiles() As String  = {"palette.png", "temp.avi",  "output.gif"}
        For x As Integer  = 0 To  2
            IO.File.Delete(tempFiles(x))
        Next
 
        Dim cmdStrings() As String  = {"-f image2 -framerate "  & fps & " -pattern_type sequence -start_number 0 -r " & fps & " -i images\frame%05d.png -vcodec mjpeg -qscale 1 temp.avi",
                                        "-i temp.avi -vf palettegen palette.png",
                                        "-i temp.avi -i palette.png -lavfi paletteuse output.gif"}
        For x As Integer  = 0 To  2
            Dim p As New  Process
            p.StartInfo.FileName = "ffmpeg.exe"
            p.StartInfo.WorkingDirectory = My.Application.Info.DirectoryPath
            p.StartInfo.Arguments = cmdStrings(x)
            p.Start()
            p.WaitForExit()
        Next
 
        IO.File.Copy("output.gif", outputFilename)
 
        MsgBox("done")
    End Sub
 
    ''' <summary>
    ''' Creates an AVI movie using images, framerate and output filename specified
    ''' </summary>
    ''' <param name="files">an array of IO.FileInfo to be used in movie</param>
    ''' <param name="outputFilename">where to save the AVI</param>
    ''' <param name="fps">the framerate</param>
    Public Shared  Sub createAVI(files() As IO.FileInfo, outputFilename As String, fps As  Integer)
        'avi
        copyFiles(files)
        IO.File.Delete("output.avi")
        Dim p As New  Process
        p.StartInfo.FileName = "ffmpeg.exe"
        p.StartInfo.WorkingDirectory = My.Application.Info.DirectoryPath
        p.StartInfo.Arguments = "-f image2 -framerate "  & fps & " -pattern_type sequence -start_number 0 -r " & fps & " -i images\frame%05d.png -vcodec mjpeg -qscale 1 output.avi"
        p.Start()
        p.WaitForExit()
 
        IO.File.Copy("output.avi", outputFilename)
 
        MsgBox("done")
    End Sub
 
End Class

Other Resources

FFmpeg - A complete, cross platform solution to record, convert and stream audio and video. 

FFmpeg on wikipedia

Download here...