1

问题(向具有奇怪字符的 avconv 发送 args)

这是一个使用“avconv”转换文件的函数:我尝试了多种方法,但是,对于我的生活,我无法让引用正常工作!

# -----------------------------------------------------------------------
#from pipes import quote # this fails in the same way as shlex.quote.
# The following is copied from python 3.3 shlex.py and modified for 2.7 -----------------------------------
import re
#_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search

def quote(s):
    """Return a shell-escaped version of the string *s*."""
    if not s:
        return "''"
    if _find_unsafe(s) is None:
        return s

    # use single quotes, and put single quotes into double quotes
    # the string $'b is then quoted as '$'"'"'b'
    return "'" + s.replace("'", "'\"'\"'") + "'"



def convert(from_fpath, to_path):

        from_fpath = quote(from_fpath)
        to_fpath = quote(to_fpath)

        cmd = ['avconv', '-i', from_fpath, to_fpath]
        print '--- avconv -i %s \t %s' % (from_fpath, to_fpath)

        try:
            #output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
            output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
            print output
        except subprocess.CalledProcessError, e:
            print "Failed", str(e)
            print "Failed output:\n", e.output
# -----------------------------------------------------------------------

通过python的失败

这是失败的一些输出......(由于某种原因,avconv 程序无法识别 args(我也尝试在一个 arg 而不是 2 中使用 '-i my_file')。

--- avconv -i '/home/me/MEDIA/MUSIC_CONVERT/Zen/05 Je T'"'"'aime Mais.m4a'   '/home/me/MEDIA/MUSIC_CONVERT/CONVERTED/05 Je T'"'"'aime Mais.mp3'
Failed Command '['avconv', '-i', '\'/home/me/MEDIA/MUSIC_CONVERT/Zen/05 Je T\'"\'"\'aime Mais.m4a\'', '\'/home/me/MEDIA/MUSIC_CONVERT/CONVERTED/05 Je T\'"\'"\'aime Mais.mp3\'']' returned non-zero exit status 1
Failed output:
avconv version 0.8.3-6:0.8.3-6ubuntu2, Copyright (c) 2000-2012 the Libav developers
  built on Oct  1 2012 12:57:14 with gcc 4.7.2
Use -h to get full help or, even better, run 'man avconv'
Hyper fast Audio and Video encoder
usage: avconv [options] [[infile options] -i infile]... {[outfile options] outfile}...

没有 python 也一样

在命令行上(即在 shell 中),该命令有效:

$ avconv -i '/home/me/MEDIA/MUSIC_CONVERT/Zen/05 Je T'"'"'aime Mais.m4a'  '/home/me/MEDIA/MUSIC_CONVERT/CONVERTED/05 Je T'"'"'aime Mais.mp3'
avconv version 0.8.3-6:0.8.3-6ubuntu2, Copyright (c) 2000-2012 the Libav developers
  built on Oct  1 2012 12:57:14 with gcc 4.7.2
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/me/MEDIA/MUSIC_CONVERT/Zen/05 Je T'aime Mais.m4a':
  Metadata:
    major_brand     : M4A 
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 2007-11-10 00:27:13
    title           : Je T'aime Mais
    artist          : Zazie
    album           : Zen
    genre           : Rock
    track           : 5/12
    disc            : 1/1
    date            : 1995
    gapless_playback: 0
    encoder         : iTunes v7.5.0.20, QuickTime 7.3
  Duration: 00:04:19.80, start: 0.000000, bitrate: 129 kb/s
    Stream #0.0(und): Audio: aac, 44100 Hz, stereo, s16, 128 kb/s
    Metadata:
      creation_time   : 2007-11-10 00:27:13

……等等……一切都好!

我正在使用 Ubuntu 12.10

4

1 回答 1

3

您可能不需要传递shell=Truesubprocess.check_output()转义cmd列表中的文件路径。

于 2012-11-08T22:19:37.517 回答