問題 (奇妙な文字を持つ 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経由のFAILURE
これが失敗の出力です...(何らかの理由で、avconvプログラムが引数を認識していません(2ではなく1つの引数で「-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なしで動作する同じこと
コマンド ライン (シェル) では、次のコマンドが機能します。
$ 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 を使用しています