使用VTE模块运行bash脚本

1 投票
1 回答
599 浏览
提问于 2025-04-18 05:58

我正在尝试使用VTE模块来运行一个bash脚本:

我遇到了几个错误,

其中一个是:

avconv version 9.11-6:9.11-2ubuntu2, Copyright (c) 2000-2013 the Libav developers
  built on Mar 24 2014 06:12:33 with gcc 4.8 (Ubuntu 4.8.2-17ubuntu1)
: No such file or directory

这是我Python脚本的一部分:

def download(self, a, donnees=None):                
      adresse = self.champ.get_text()
      self.v.fork_command('./pluzz.sh', '-u', adresse)  # calling the bash script
[...]
def __init__(self):       
      self.v = vte.Terminal()
      self.v.set_emulation('xterm')
      self.v.show()
      self.box1.add(self.v)

还有一部分bash脚本:

echo -e "$VERT""DEBUT DU TRAITEMENT""$NORMAL"

#Recuperation de l' ID de l' emission
UserAgent='Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0'
ID=$(wget -q -U "${UserAgent}" "${URL}" -O - | grep -E "og:url.*content.*http://*" | sed 's+.*,\([0-9]*\).*+\1+g')

#wget du json conteant les infos
echo -e "$ROSE""-->RECUPERATION DU JSON""$NORMAL"
JSON="$(wget -q -U "${UserAgent}" "http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=${ID}&catalogue=Pluzz&callback=webserviceCallback_${ID}" -O - | sed 's+\\/+/+g')"

#Recuperation des infos
echo -e "$ROSE""-->TRAITEMENT DU JSON""$NORMAL"
DATE="$(echo "${JSON}" | sed 's+.*date_debut..\"\([^\"]*\)\".*+\1+g')"
PROG="$(echo "${JSON}" | sed 's+.*code_programme..\"\([^\"]*\)\".*+\1+g')"
M3U="$(echo "${JSON}" | sed 's+.*url..\"\([^\"]*m3u8\)\".*+\1+g')"

#Recuperation du master M3U et traitement
echo -e "$BLEU""-->RECUPERATION DU FICHIER VIDEO""$NORMAL"
M3U2="$(wget -q -U "'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0'" "${M3U}" -O - | grep -E ".*index_2.*")"

avconv -i "${M3U2}" -vcodec copy -acodec copy "${PROG}_${ID}.mkv"

这个bash脚本在控制台中运行得很好:

:~./pluzz.sh http://pluzz.francetv.fr/videos/coluche_un_clown_ennemi_d_etat.html
DEBUT DU TRAITEMENT
-->RECUPERATION DU JSON
-->TRAITEMENT DU JSON
-->RECUPERATION DU FICHIER VIDEO
avconv version 9.11-6:9.11-2ubuntu2, Copyright (c) 2000-2013 the Libav developers
  built on Mar 24 2014 06:12:33 with gcc 4.8 (Ubuntu 4.8.2-17ubuntu1)
[hls,applehttp @ 0x1ebfe00] max_analyze_duration reached
Input #0, hls,applehttp, from 'http://ftvodhdsecz-f.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2014/S18/J5/101152365-20140502-,398,632,934,k.mp4.csmil/index_2_av.m3u8?null=':
  Duration: 00:56:10.00, start: 0.100667, bitrate: 0 kb/s
    Stream #0.0: Video: h264 (Main), yuv420p, 704x396 [PAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0.1: Audio: aac, 48000 Hz, stereo, fltp
    Stream #0.2: Data: [21][0][0][0] / 0x0015

我希望我的解释很清楚……

谢谢

编辑:我找到了解决方案:将

self.v.fork_command('./pluzz.sh', '-u', adresse)

替换为

self.v.fork_command(None, ['/bin/bash', '-u', './pluzz.sh', adresse])

1 个回答

1

我猜你的问题可能是因为:

  self.v.fork_command('./pluzz.sh', '-u', adresse)  # calling the bash script

这有两个假设:

  1. 脚本和应用程序是在同一个文件夹里启动的;
  2. 脚本是可以执行的,并且可以独立运行。

但是对于第二点,脚本缺少 #!/bin/bash 这个开头部分,这个部分是告诉系统这是一个需要执行的脚本。而对于第一点,你最好使用一个绝对路径,或者是相对于当前模块文件的路径。

你应该使用相对于当前脚本的路径,或者一个绝对路径:

import sys
import os
### if the pluzz script is in same directory as your python app
pluzz_script = os.path.join(os.path.dirname(sys.argv[0]), 'pluzz.sh')
### explicitely run bash, to have it run your script
self.v.fork_command('/bin/bash', pluzz_script, '-u', address)

补充:重新读了一遍你的帖子,发现其实不是这样。不过你还是应该听从我之前的建议,以避免将来在分发脚本时出现问题。

你的问题实际上是文件没有被下载,或者无法写入,导致 avconv 无法访问它。很难判断具体哪里出错,因为脚本没有显示 wget 的输出。不过你最好把这个脚本转换成 Python,并使用一个安全的临时目录来下载文件并进行处理。

这是你脚本的 Python 版本:

# echo -e "$VERT""DEBUT DU TRAITEMENT""$NORMAL"
# retrieval of the show's id

from lxml import etree
import subprocess
import requests
import json
import os

### User defined values:
url='http://pluzz.francetv.fr/videos/doctor_who.html'
target_path=os.path.join(os.environ['HOME'], 'Downloads')
###

headers={'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0'}

##### ID=$(wget -q -U "${UserAgent}" "${URL}" -O - | grep -E "og:url.*content.*http://*" | sed 's+.*,\([0-9]*\).*+\1+g')
p = etree.HTML(requests.get(url, headers=headers).text)
show_id = p.xpath('//meta[@property="og:url"]/@content')[0].split(',')[-1].split('.')[0]

##### get the JSON containing the show's data
##### JSON="$(wget -q -U "${UserAgent}" "http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=${ID}&catalogue=Pluzz&callback=webserviceCallback_${ID}" -O - | sed 's+\\/+/+g')"
show_data_url = "http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion={show}&catalogue=Pluzz&callback=webserviceCallback_{show}"
show_data = json.loads("".join(requests.get(show_data_url.format(show=show_id), headers=headers).text.split('(')[1:])[:-1])

# retrieve data from the json
##### DATE="$(echo "${JSON}" | sed 's+.*date_debut..\"\([^\"]*\)\".*+\1+g')"
##### PROG="$(echo "${JSON}" | sed 's+.*code_programme..\"\([^\"]*\)\".*+\1+g')"
##### M3U="$(echo "${JSON}" | sed 's+.*url..\"\([^\"]*m3u8\)\".*+\1+g')"
# date = show_data['diffusion']['date_debut']
# prog = show_data['code_programme']
# m3u = list(filter(lambda x: x['format'] == 'm3u8-download', j['videos']))[0]['url']

p = requests.get(list(filter(lambda x: x['format'] == 'm3u8-download', show_data['videos']))[0]['url'], headers=headers).text

# M3U retrieval
##### M3U2="$(wget -q "${M3U}" -O - | grep -E ".*index_2.*")"
video_url = list(filter(lambda l: "index_2" in l, p.split()))[0]

##### avconv -i "${M3U2}" -vcodec copy -acodec copy "${PROG}_${ID}.mkv"

dest_file = "{}_{}.mkv".format(show_data['code_programme'], show_id)
subprocess.call(['avconv', '-i', video_url, '-vcodec', 'copy', '-acodec', 'copy', os.path.join(target_path, dest_file)])

或者你可以用来处理 avconv 输出的:

p = subprocess.Popen(['avconv', '-i', video_url, '-vcodec', 'copy', '-acodec', 'copy', os.path.join(target_path, dest_file)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

for line in out:
    print(out)

这样你就可以在应用程序的界面中构建一个进度条,而不是在终端中显示难看的输出。

我还做了一个代码的另一个版本,设计更好,有命令行参数解析器,并处理输出行以显示进度条:

希望对你有帮助。

撰写回答