1

我编写了一个应用程序,它使用 ffmpeg 将文件转换为特定文件夹中的不同格式。该文件夹包含 .mpg、.mp3、.avi、.flv ... 或任何决定添加的音频/视频文件。当我使用我的应用程序选择名称中包含空格的要转换的文件时,就会出现问题;该应用程序会立即关闭,而无需将文件转换为其他格式。目前,我必须重命名名称中包含空格的所有文件,然后才能通过我的 app->ffmpeg 进行转换。我正在尝试编写一个函数,该函数将从我的应用程序开始检查特定文件夹中的文件名是否有空格,用下划线('_')替换这些空格。我在使用正则表达式时遇到了一些问题;我不擅长编写/配置它们来解决问题。有人可以建议我使用正确的正则表达式来查找文件名中的空白/空格吗?以下是我到目前为止的内容:

import os
import re


pattern = r"([^\s]+(?=\.(mp3|mov|mpg|mp4|flv|avi|mpeg4|mkv|mpeg|mpg2|.wav))\.\2)"


def replace_Wspace(self, fName):
    if re.match(pattern, fName):
        fname = fName.replace(' ', '_')
    return fname

我正在根据要求从我的应用程序中添加处理对 ffmpeg 的调用的代码部分:

def convertButton(self, e):

    unit1 = self.format_combo1.GetValue()
    #Media Formats
    unit2 = self.format_combo2.GetValue()
    unit3 = self.format_combo3.GetValue()
    unit4 = None
    unit5 = self.format_combo5.GetValue()
    bitRate = self.format_combo6.GetValue()
    unit6 = bitRate
    if unit3 == '-qmax':
        unit4 = self.format_combo4.GetValue()
    else:
        pass

    os.chdir("c:\\d-Converter\\ffmpeg\\bin")
    wrkdir = os.getcwd()

    newfile = unit1
    stripped = newfile.strip('mpeg3aviovfl4w2c.') #Strips the extension from the original file name


    progname='c:\\d-Converter\\ffmpeg\\bin\\ffmpeg.exe' + ' -i '

    preset1_a='-vn -ar 44100 -ac 2 -ab'
    preset1_b='-f mp3 '
    preset_mp3='.mp3'

    chck_unit1 = self.my_endswith(unit1)



    while True:    
        if unit5 == 'video to mp3':

            if unit6 == 'k/bs' or unit6 == '':
                amsg = wx.MessageDialog(None, 'You must select a bit rate.', 'Media Converter', wx.ICON_INFORMATION)
                amsg.ShowModal()
                amsg.Destroy()
                break

            elif unit5 == 'video to mp3' and unit6 != 'k/bs' or unit6 != '':
                self.button.Disable()
                self.button2.Enable()
                self.format_combo1.Disable()
                self.format_combo2.Disable()
                self.format_combo3.Disable()
                self.format_combo4.Disable()
                self.format_combo5.Disable()
                self.format_combo6.Disable()
                startWorker(self.LongTaskDone, self.LongTask3, wargs=(progname, wrkdir, unit1, preset1_a, unit6, preset1_b, stripped, preset_mp3))
                break
            elif unit1 != unit1.endswith(".mpg") or unit1.endswith(".mpeg") or unit1.endswith(".avi") or unit1.endswith(".mp4") or unit1.endswith(".flv"):
                bmsg = wx.MessageDialog(None, 'You must select a valid format to convert to .mp3.', 'Media Converter', wx.ICON_INFORMATION)
                bmsg.ShowModal()
                bmsg.Destroy()
                break

        else:
            pass



        if unit1 == 'Select Media' or unit1 == '':
            amsg = wx.MessageDialog(None, 'You must select a media file!', 'Media Converter', wx.ICON_INFORMATION)
            amsg.ShowModal()
            amsg.Destroy()
            break


        elif unit2 == 'Select Format' or unit2 == '' or unit2 == chck_unit1:
            amsg = wx.MessageDialog(None, 'You must select a valid format', 'Media Converter', wx.ICON_INFORMATION)
            amsg.ShowModal()
            amsg.Destroy()
            break


        elif unit3 == 'Select Quality' or unit3 == '':
            amsg = wx.MessageDialog(None, 'You must select quality', 'Media Converter', wx.ICON_INFORMATION)
            amsg.ShowModal()
            amsg.Destroy()
            break

        elif unit3 != 'Select Quality' or unit3 != '':
            self.format_combo5.Disable()

            if unit3 == '-qmax':
                if unit4 == '0' or unit4 == '':
                    amsg = wx.MessageDialog(None, 'You must select number between 1-8.', 'Media Converter', wx.ICON_INFORMATION)
                    amsg.ShowModal()
                    amsg.Destroy()
                    break
                else:
                    self.button.Disable()
                    self.button2.Enable()
                    self.format_combo1.Disable()
                    self.format_combo2.Disable()
                    self.format_combo3.Disable()
                    self.format_combo4.Disable()
                    self.format_combo5.Disable()
                    startWorker(self.LongTaskDone, self.LongTask2, wargs=(progname,wrkdir,unit1,unit3,unit4,stripped,unit2))
                    break
            elif unit3 == '-sameq':
                self.button.Disable()
                self.button2.Enable()
                self.format_combo1.Disable()
                self.format_combo2.Disable()
                self.format_combo3.Disable()
                self.format_combo4.Disable()
                self.format_combo5.Disable()
                startWorker(self.LongTaskDone, self.LongTask, wargs=(progname,wrkdir,unit1,unit3,stripped,unit2))
                break   




def LongTask(self, progname, wrkdir, unit1, unit3, stripped, unit2):
    convert_file1 = progname + wrkdir + '\\' + unit1 + ' ' + unit3 + ' ' + stripped + unit2
    self.statusbar.SetStatusText("Converting: " + unit1 + "...")
    os.system(convert_file1)
    print convert_file1


def LongTask2(self, progname, wrkdir, unit1, unit3, unit4, stripped, unit2):
    convert_file2 = progname + wrkdir + '\\' + unit1 + ' ' + unit3 + ' ' + unit4 + ' ' + stripped + unit2
    self.statusbar.SetStatusText("Converting: " + unit1 + "...")
    os.system(convert_file2)


def LongTask3(self, progname, wrkdir, unit1, preset1_a, unit6, preset1_b, stripped, preset_mp3):
    convert_file3 = progname + wrkdir + '\\' + unit1 + ' ' + preset1_a + ' ' + unit6 + ' ' + preset1_b + stripped + preset_mp3
    self.statusbar.SetStatusText("Converting: " + unit1 + "...")
    os.system(convert_file3)
    print convert_file3

def LongTask4(self, progdir, wrkdir, prog_dir, progdir3, f_string, s_string2, vid_format):
    convert_file4 = progdir + f_string + prog_dir + s_string2 + progdir3 + f_string.strip('mpegaviw24ofl.') + vid_format
    self.statusbar.SetStatusText("Converting: " + f_string + "...")
    os.system(convert_file4)
    print convert_file4
4

3 回答 3

6

代替正则表达式,使用 use ,您可以提供一个元组作为所有音频扩展的参数,如果以其中任何一个结尾,fName.endswith()它将返回,例如:TruefName

audio = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
if fName.endswith(audio) and ' ' in fName:
    return fName.replace(' ', '_')
return fName

如果没有空格或有不同的扩展名,它将返回原始字符串而不做任何更改。

或者,您可以使用os.path.splitext()

audio = set(('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav'))
if os.path.splitext(fName)[1] in audio and ' ' in fName:
    return fName.replace(' ', '_')
return fName
于 2013-01-07T18:18:34.133 回答
1

我认为您不需要正则表达式。你可以简单地使用if ' ' in file:

像这样:

import os
dir = '.'
for file in os.listdir(dir)
  if ' ' in file:
    os.rename(file,file.replace(' ','_'))

在尝试批量重命名之前,可能要print file测试您是否位于正确的目录中。

如果您需要确保它是可接受的文件格式,请使用 os.path.splitext() 获取扩展名:

extensions = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
fname, ext = os.path.splitext(file)
if ext in extensions:
  ...
于 2013-01-07T18:18:17.343 回答
1

FJ的回答很棒。如果您真的很关心空间,并且确实想像 iTunes 一样更改它们,我只想补充一点,您可能想做这样的事情:

audio = ('.mp3','.mov','.mpg','.mp4','.flv','.avi','.mpeg4','.mkv','.mpeg','.mpg2','.wav')
spaces=['\t',' ','\r','\n','\v']  # etc -- other nasty space like characters...
if fName.endswith(audio) and any(space in fName for space in spaces):
    return ''.join(c if c not in spaces else '_' for c in fName)
...

您正在将 fName 中的所有潜在空间(如字符)捕获到“_”中

因此,如果您有:

fName='file name\twith\nstuff\vin it'

它变成:

file_name_with_stuff_in_it     
于 2013-01-07T20:01:16.437 回答