gpt4 book ai didi

python arg 在 shell 命令中转义(调用 avconv)

转载 作者:行者123 更新时间:2023-11-30 23:39:46 25 4
gpt4 key购买 nike

问题(将具有奇怪字符的参数发送到 avconv)

这是一个使用“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 程序无法识别参数(我也尝试在一个参数而不是 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

最佳答案

您可能不需要将 shell=True 传递给 subprocess.check_output() ,也不需要转义 cmd 中的文件路径列表。

关于python arg 在 shell 命令中转义(调用 avconv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com