【问题标题】:& operator string error with string of arguments to processStartInfo& 运算符字符串错误,带有 processStartInfo 的参数字符串
【发布时间】:2015-07-03 08:52:51
【问题描述】:

我需要将一个字符串传递给 ffmpeg 的 ProcessStartInfo 以便我可以调用 dim.Argument 并让它在我解析文件时附加代码中的字符串和变量。

我有代码在movedFileInfo 中看到的当前文件名.mp3,但它不允许我使用& 运算符将其附加到字符串...帮助?

我知道可能还有另一种方法可以在单独的函数中使用 ffmpeg 命令来简单地使用“for”循环遍历目录,但我没有找到运行我的 ffmpeg.exe 或 ffprompt 的成功命令视窗。当我写信给merge.txt但找不到示例时,我还需要附加一个回车符......我是vba的新手。

这些命令有效,但 vba 抱怨我的字符串中的 & 运算符,错误为 The operator '&' is not defined for types 'String' and 'System.IO.FileInfo'

所以我的理解是,我传递给psi.Arguments 的字符串不喜欢我向它发送一个字符串和一个使用& 运算符附加的变量的事实......我只是使用逗号或如何将变量 movedFileInfo 附加到 ffmpeg -i? psi 上面定义为 ProcessStartInfo...所以我不确定 vb 可以识别哪些类型...我还没有找到有关 ProcessStartInfo 的信息来启动我的 ffmpeg exe。

见下面的代码:

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

【问题讨论】:

  • 当你说它'抱怨我的 & 运算符'时,你得到什么错误信息?
  • 运算符 '&' 没有为类型 'String' 和 'System.IO.FileInfo' 定义。所以我的理解是我传递给 psi.Arguments 的字符串不喜欢我发送一个字符串和一个使用“&”运算符附加的变量的事实......我只是使用逗号还是如何将变量movedFileInfo附加到ffmpeg -i? psi 在上面被定义为 ProcessStartInfo...所以我不确定 vb 可以识别什么类型...我还没有找到有关 ProcessStartInfo 的信息来启动我的 ffmpeg exe。
  • 谢谢。您还应该编辑您的问题以包含此信息,以便其他回答者更容易看到。
  • 在上面添加了我的问题并更新了我的帖子以包含完整的代码,这样您就可以简单地复制/粘贴和排除故障...
  • 我建议将您的问题分开(StackOverflow 的政策是关闭过于宽泛的问题)。所以请保留这个关于字符串连接问题的问题,并提出一个关于将 CR 附加到文本文件的新问题(在搜索以确保它还没有被问到之后!)

标签: vb.net operator-keyword string-concatenation processstartinfo


【解决方案1】:

问题在于 VB 中的 &amp; 运算符仅用于连接 System.String 对象。它比使用+ 连接更严格。您不能使用它将StringSystem.IO.FileInfo 对象连接在一起。您想要的是从 FileInfo 对象中获取文件名。它有一个属性FileInfo.FullName,它将以String 的形式返回文件的完整路径——来自documentation

试试吧:

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

如果您想用.mp4 替换.mts 扩展名,而不是像此代码那样简单地附加.mp4,请参阅this question

【讨论】:

  • 这适用于转换为字符串,但随后将文件转换为 .MTS.MP4 而不是使用movedFileInfo & ".mp4"...是否还有其他选项可以将最后一部分转换为字符串不知何故但不使用文件类型 .MTS?
  • 谢谢。如果对您有用,请单击答案左侧的勾号将其标记为“已接受”。正如我在回答中提到的,更改扩展名(而不是附加 mp4)是一个不同的问题,答案是 here。我将编辑我的问题以包含链接。
  • 完美谢谢!在此期间,我只是使用了一个 temp,然后调用了一个 temp+=1,因为此时我是否维护文件名并不重要……我遇到的下一个问题是进度条,所以我会在这里搜索下一个。
猜你喜欢
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 2012-10-09
相关资源
最近更新 更多