Python Pydub Permission被拒绝了?

时间:2017-05-27 10:28:08

标签: python-2.7 windows-7

当我运行此代码时:

import re
import urllib2
import calendar
import datetime
import getopt
import sys
import time

crumble_link = 'https://finance.yahoo.com/quote/{0}/history?p={0}'
crumble_regex = r'CrumbStore":{"crumb":"(.*?)"}'
cookie_regex = r'Set-Cookie: (.*?); '
quote_link = 'https://query1.finance.yahoo.com/v7/finance/download/{}?period1={}&period2={}&interval=1d&events=history&crumb={}'


def get_crumble_and_cookie(symbol):
    link = crumble_link.format(symbol)
    response = urllib2.urlopen(link)
    match = re.search(cookie_regex, str(response.info()))
    cookie_str = match.group(1)
    text = response.read()
    match = re.search(crumble_regex, text)
    crumble_str = match.group(1)
    return crumble_str, cookie_str


def download_quote(symbol, date_from, date_to):
    time_stamp_from = calendar.timegm(datetime.datetime.strptime(date_from, "%Y-%m-%d").timetuple())
    time_stamp_to = calendar.timegm(datetime.datetime.strptime(date_to, "%Y-%m-%d").timetuple())

    attempts = 0
    while attempts < 5:
        crumble_str, cookie_str = get_crumble_and_cookie(symbol)
        link = quote_link.format(symbol, time_stamp_from, time_stamp_to, crumble_str)
        #print link
        r = urllib2.Request(link, headers={'Cookie': cookie_str})

        try:
            response = urllib2.urlopen(r)
            text = response.read()
            print "{} downloaded".format(symbol)
            return text
        except urllib2.URLError:
            print "{} failed at attempt # {}".format(symbol, attempts)
            attempts += 1
            time.sleep(2*attempts)
    return ""

if __name__ == '__main__':
    print get_crumble_and_cookie('KO')
    from_arg = "from"
    to_arg = "to"
    symbol_arg = "symbol"
    output_arg = "o"
    opt_list = (from_arg+"=", to_arg+"=", symbol_arg+"=")
    try:
        options, args = getopt.getopt(sys.argv[1:],output_arg+":",opt_list)
    except getopt.GetoptError as err:
        print err

    for opt, value in options:
        if opt[2:] == from_arg:
            from_val = value
        elif opt[2:] == to_arg:
            to_val = value
        elif opt[2:] == symbol_arg:
            symbol_val = value
        elif opt[1:] == output_arg:
            output_val = value

    print "downloading {}".format(symbol_val)
    text = download_quote(symbol_val, from_val, to_val)

    with open(output_val, 'wb') as f:
        f.write(text)
    print "{} written to {}".format(symbol_val, output_val)

弹出一个ffmpeg窗口,我收到此错误: Error Pic

即使我使用admin privilleges运行它: Error with admin privilleges

  

注意:
   我尝试导出的每个位置都会出现错误

3 个答案:

答案 0 :(得分:1)

请参见此线程here。他们建议安装simpleaudio(pip安装简单音频)来解决此问题。它对我有用。

答案 1 :(得分:1)

如果您使用的是Windows,则遇到此问题,并且在安装simpleaudio时遇到问题,则可以尝试安装pyaudio

如果您使用的是Anaconda,则可以通过以下方式安装pyaudio

conda install -c anaconda pyaudio

对我来说,Anaconda上的simpleaudio仅适用于Linux和MacOS,不适用于Windows。

答案 2 :(得分:0)

from pydub.playback import play
from pydub import AudioSegment
AudioSegment.converter = r"C:\\ffmpeg\\bin\\ffmpeg.exe"
AudioSegment.ffmpeg = r"C:\\ffmpeg\\bin\\ffmpeg.exe"
myAudioFile = 'C:\\Users\\User\\Documents\\UNDAR\\CepreUNDAR\\Shema.mp3'
print(myAudioFile)
sound_stereo = AudioSegment.from_mp3(myAudioFile) 
sound_monoL = sound_stereo.split_to_mono()[0]
sound_monoR = sound_stereo.split_to_mono()[1]
# Invert phase of the Right audio file
sound_monoR_inv = sound_monoR.invert_phase()
# Merge two L and R_inv files, this cancels out the centers
sound_CentersOut = sound_monoL.overlay(sound_monoR_inv)
# Export merged audio file
myAudioFile_CentersOut = "ShemaDrumless.mp3"
fh = sound_CentersOut.export(myAudioFile_CentersOut, format="mp3")