0

I need to pass a string into the ProcessStartInfo for ffmpeg so that I can call the dim.Argument and have it append the strings and variables from code as I parse through the files.

I have the current filename.mp3 that the code sees in movedFileInfo but it won't allow me to append it to the string using & operators... help?

I know there may be one other way to do this utilizing the ffmpeg command in separate function to simply loop through a directory using "for" but I've not found a successful command for running my ffmpeg.exe nor the ffprompt in windows. I also need to append a carriage return when I write to merge.txt but can't find an example... I'm new to vba.

These commands work but vba is complaining about the & operator in my string with the error The operator '&' is not defined for types 'String' and 'System.IO.FileInfo'.

So what I understand is that the string I'm passing into psi.Arguments doesn't like the fact that I'm sending it a string and a variable appended using the & operator... do I simply use a comma or how do I append the variable movedFileInfo to ffmpeg -i? psi is defined above as ProcessStartInfo... so I'm not sure what types vb recognizes for it... I haven't found info on ProcessStartInfo to kick off my ffmpeg exe.

See code below:

Imports System
Imports System.IO
Imports System.Text.RegularExpressions


Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        'videos are from SD card always on I:\Private\AVCHD\BDMV\STREAM\
        'store files to RAID drives in folders structured as :
        '       F:\BUILD\FLIGHT#\CAM#\<TAIL_NUMBER>_FLT_<FLT_NUMBER>_UTC_<START_UTC_TIME>_CAM_<CAM_NUMBER>.MTS

        'set base dir as directory F:\
        Dim dir As String = "C:\"
        Dim video_card_dir As String = "C:\1bak\" '"I:\Private\AVCHD\BDMV\STREAM\"
        Directory.SetCurrentDirectory(dir)

        Dim new_flightnum_directory = dir & Me.BUILD.Text & "\" & FLT.Text & "\"
        'establish new video dir>  F: \    BUILD      \     FLIGHT # \               CAM #    \
        Dim new_video_directory = dir & Me.BUILD.Text & "\" & FLT.Text & "\" & Me.CAM.Text & "\"
        'establish new filename to rename the video file
        '                               TAIL #              FLT #
        Dim new_filename As String = TAIL.Text & "_" & FLT.Text & "_" & UTC.Text & "_" & CAM.Text
        Dim ffmpeg As String = "C:\ffmpeg\bin\ffmpeg.exe"
        '****FFMPEG required variables
        Dim psi As ProcessStartInfo = New ProcessStartInfo("C:\ffmpeg\bin\ffmpeg.exe")
        Dim proc As Process = Process.Start(psi)

        psi.UseShellExecute = True
        psi.CreateNoWindow = True
        '****end FFMPEG required variables



        '!!!!!!!!!!!!!!!!!!!!!!!!!!!need to add the processing below to the IF statement aboev so that if the folders exist, the video processing doesn't attempt to run on existing files
        ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!





        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~START - MOVE THIS DOWN BELOW CREATION OF FILE STRUCTURE WHEN DOEN DEBUGGING************


        '***START MOVING the files from the card to the new directory****
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")

            Dim foundFileInfo As New System.IO.FileInfo(foundFile)
            My.Computer.FileSystem.MoveFile(foundFile, new_video_directory & foundFileInfo.Name)


        Next

        For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")

            Dim movedFileInfo As New System.IO.FileInfo(foundFile)
            psi.Arguments = "ffmpeg -i " & movedFileInfo & " -vcodec -c libx264 " & movedFileInfo & ".mp4"
            psi.ToString()

            'proc = Process.Start(psi)

            '***convert each MTS file in the new directory to MP4****
            'Writes filenames to merge.txt as " path\to\merge.txt ,               'file '  F:\path\to\               file1  .MP4         '"  so that ffmpeg can merge, then rename
            'My.Computer.FileSystem.WriteAllText(new_video_directory & "merge.txt", "file '" & movedFileInfo & ".mp4'" & vbCrLf, True)
            '>>>>need to add carriage return to text file

            'NOW CAPTURE FILENAMES OF MP4 and MERGE INTO 1 MP4 FILE

            '   merge all F:\path\to\merge.txt to merge the files & merge them 
            'psi.Arguments = "ffmpeg -f concat -i " & new_video_directory & "merge.txt -c copy " & new_filename & ".mp4"
            proc = Process.Start(psi)

        Next

        '***END MERGE FILES***


        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~* END - MOVE


        '***START CREATE STORAGE DIRECTORY STRUCTURE ***
        'Verify if the build # directory exists?
        If My.Computer.FileSystem.DirectoryExists(dir & Me.BUILD.Text) Then
            MessageBox.Show("The build directory exists, moving on to create subdirectories")
        Else
            Try
                'create the new directory                F:\    build \ FLIGHT #
                My.Computer.FileSystem.CreateDirectory(dir & Me.BUILD.Text)
                MessageBox.Show("The build directory" & dir & Me.BUILD.Text & " was created.")
            Catch ex As Exception
                MessageBox.Show("Doh! The build directory could not be created!  Error: " & ex.Message, "Error creating directory.", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End If

        'verify if the flight num directory exists - or create it
        If My.Computer.FileSystem.DirectoryExists(new_flightnum_directory) Then
            MessageBox.Show("The flight # folder already exists!  Check that you have the right Flight #.")
        Else
            Try
                'create the new directory                F:\  BUILD \  FLIGHT #
                My.Computer.FileSystem.CreateDirectory(new_flightnum_directory)

                'Now create new subDirectories   
                My.Computer.FileSystem.CreateDirectory(new_video_directory)

                MessageBox.Show("The new flight directory & video CAM subdirectories have been created!  The videos will be moved and files converted now which will take some time.")

            Catch ex As Exception
                MessageBox.Show("Doh! The flight num or CAM directory could not be created!  Error: " & ex.Message, "Error creating directory.", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If


        '***END CREATE STORAGE DIRECTORY STRUCTURE ***   





        MessageBox.Show("new merged video file has been created in " & dir & Me.BUILD.Text & "\" & Me.CAM.Text)

    End Sub

End Class
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • When you say it 'complains about my & operator', what error message are you getting? – Aaron D Apr 24 '15 at 16:35
  • The operator '&' is not defined for types 'String' and 'System.IO.FileInfo'. So what's I understand is that the string I'm passing into psi.Arguments doesn't like the fact that I'm sending it a string and a variable appended using the '&' operator... do I simply use a comma or how do I append the variable movedFileInfo to ffmpeg -i? psi is defined above as ProcessStartInfo... so I'm not sure what types vb recognizes for it... I haven't found info on ProcessStartInfo to kick off my ffmpeg exe. – Robert McCorkle Apr 24 '15 at 16:49
  • Thanks. You should also edit your question to include this information so it's easier for other answerers to see. – Aaron D Apr 24 '15 at 16:58
  • added my question above and updated my post to include the full code so you can simply copy/paste and troubleshoot... – Robert McCorkle Apr 24 '15 at 17:33
  • I recommend separating your questions (StackOverflow has a policy of closing questions that are too broad). So please keep this question about the String concatenation problem, and ask a new question about appending CR to a text file (after searching to make sure it hasn't been asked already!) – Aaron D Apr 25 '15 at 01:42

1 Answers1

2

The problem is that the & operator in VB is used only to concatenate System.String objects. It's stricter than using + to concatenate. You can't use it to concatenate a String and a System.IO.FileInfo object together. What you want is to get the file name from the FileInfo object. It has an attribute, FileInfo.FullName which will return the full path of the file as a String - from the documentation.

Try instead:

For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")
    Dim movedFileInfo As New System.IO.FileInfo(foundFile)
    psi.Arguments = "ffmpeg -i " & movedFileInfo.FullName & " -vcodec -c libx264 " & movedFileInfo.FullName & ".mp4"
    psi.ToString()
    proc = Process.Start(psi)
Next

If you want to replace the .mts extension with .mp4, instead of simply appending .mp4 as this code does, please see this question.

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48
  • this works to convert to string but then converts the file to a .MTS.MP4 instead of using the movedFileInfo & ".mp4"... is there another options to convert the last part to a string somehow but not utilize the file type .MTS? – Robert McCorkle Apr 27 '15 at 22:15
  • Thanks. If it worked for you, please click the tick on the left side of the answer to mark it as "accepted". As I mentioned in my answer, changing the extension (instead of appending mp4 to it) is a different problem, which is answered [here](http://stackoverflow.com/questions/7356205/remove-file-extension-from-a-string-of-a-file). I'll edit my question to include the link as well. – Aaron D Apr 27 '15 at 23:58
  • perfect thx! In the interim I just utilized a temp and then called a temp+=1 since it doesn't really matter if I maintain the filename at this point... the next problem I have is a progress bar so I'll search here for that next. – Robert McCorkle Apr 28 '15 at 00:01