0

Mu の目的は、URL とファイル パスを指定して、25 MB のビデオ ファイル (この場合は TiVo から) をダウンロードまたはバッファリングし、プレーヤーでビデオを起動して残りのビデオのダウンロードを続行する関数を作成することです。ビデオの再生中。

ここのコードが役立つことがわかりました。これは良いスタートのようです。これは私にとって初めての Python プロジェクトなので、学習プロセスでした。これが私がこれまでに持っているコードです:

import os
import sys
import logging
import requests as REQ
from requests.auth import HTTPDigestAuth;
import datetime as DT
try:
        import cElementTree as ET
except ImportError:
        try:
                import xml.etree.cElementTree as ET
        except ImportError:
                exit_err("Failed to import cElementTree from any known place");

# Functions

def fetchSelection(url, fp):
        print('Requesting ' + url + '...');
        r = REQ.get(url, auth=HTTPDigestAuth('tivo', mak), verify=False, stream=True);
        print('Fetching your selection...');
        if r.status_code == REQ.codes.ok:
                print('HTTP Request sent, awaiting response... 200 OK');
                print('Buffering...');
        else:
                print('HTTP Request sent, awaiting response... ' + str(r.status_code));
                print(r.raise_for_status());
                return;

        video_file_size_start = 0;
        video_file_size_end = 1048576 * cacheSize;  # end in CacheSize in  MB
        block_size = 1024;

        with open(fp, 'wb') as fd:
                for chunk in r.iter_content(block_size):
                        video_file_size_start += len(chunk);
                        if video_file_size_start > video_file_size_end:
                                break;
                        fd.write(chunk);
                        print(str(video_file_size_start/1024/1024) + ' MB / ' + str(video_file_size_end/1024/1024) + ' MB');
                fd.close();

        return;

このコードが後で使用される場所は次のとおりです。

filePath = downloadPath + details[6] + '.tivo';
fetchSelection(nUrl + '&Format=' + videoFormat, filePath);
print('Launching player...');
playerCommand = 'tivodecode -m ' + mak + ' ' + filePath + ' | ';
playerCommand += 'mplayer -vf pp=lb - cache 32768 -';
os.system(playerCommand);
nType = 0;

スクリプトを実行すると、次の出力が得られます。

Requesting http://192.168.1.102:80/download/Adventure%20Time.TiVo?Container=%2FNowPlaying&id=653115&Format=video/x-tivo-mpeg...
Fetching your selection...
HTTP Request sent, awaiting response... 503
Traceback (most recent call last):
  File "test_read_nowPlaying2.py", line 186, in <module>
    fetchSelection(details[1] + '&Format=' + videoFormat, filePath);
  File "test_read_nowPlaying2.py", line 128, in fetchSelection
    print(r.raise_for_status());
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 765, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 503 Server Error: Server Busy

編集: 503 応答を受け取った理由がわかりました。正しいファイルを呼び出していませんでした。のビデオ形式をリクエストする必要がありましたvideo/x-tivo-mpeg-ts

これは進行中の作業であり、どのように進めればよいかわかりません。また、プレーヤーの再生中にダウンロードを続行する方法もわかりません。

4

1 に答える 1

0

私は必要な解決策を見つけたと思います。それは別のSO投稿から来ました: python wmv stream download

import os
import sys
import subprocess
from subprocess import Popen, PIPE
import logging
import requests as REQ
from requests.auth import HTTPDigestAuth;
import datetime as DT
try:
        import cElementTree as ET
except ImportError:
        try:
                # Python 2.5 need to import a different module
                import xml.etree.cElementTree as ET
        except ImportError:
                exit_err("Failed to import cElementTree from any known place");
from progressbar import ProgressBar, SimpleProgress

# Functions

def bufferSelection(itemUrl, fp):
        print('Fetching ' + itemUrl + '...');

        video_file_size_start = 0;
        video_file_size_buffer = 1048576 * cacheSize;  # end in CacheSize in  MB
        video_file_size_end = 0;
        block_size = 1024;

        r = REQ.get(itemUrl, auth=HTTPDigestAuth('tivo', mak), verify=False, stream=True);
        if r.status_code == REQ.codes.ok:
                print('HTTP Request sent, awaiting response... 200 OK');
                if r.headers['tivo-estimated-length'] != None:
                        video_file_size_end = (int(r.headers['tivo-estimated-length'])/1024)/1024;
                print('Downloading {} MB...'.format(video_file_size_end));
        else:
                print('HTTP Request sent, awaiting response... ' + str(r.status_code));
                print(r.raise_for_status());
                return;
        with open(fp+'.tivo', 'wb') as fd:
                pbar = ProgressBar(widgets=['Downloading: ', SimpleProgress(), ' MB'], maxval=video_file_size_end).start();
                subProcessChild = None;
                for chunk in r.iter_content(block_size):
                        if not chunk:
                                fd.close();
                                break;
                        video_file_size_start += len(chunk);
                        if video_file_size_start == video_file_size_buffer:
                                child = subprocess.Popen('tivodecode -m ' + mak + ' \"' + fp + '.tivo\" | vlc --file-caching=2048 -');
                                subProcessChild = child.poll();
                        fd.write(chunk);
                        pbar.update((video_file_size_start/1024)/1024);
                        if subProcessChild is not None:
                                fd.close();
                                os.remove(fp);
                                break;
                pbar.finish();
        print('Download complete. Running decode: \"tivodecode -m ' + mak + ' \"' + fp + '.tivo\" | vlc -vvv --file-caching=2048 -');
        child = subprocess.Popen('tivodecode -m ' + mak + ' \"' + fp + '.tivo\" | vlc -vvv --file-caching=2048 -', shell=True);
        return;

def fetchSelection(url, fp):
        playerCommand = 'curl --anyauth --globaloff --user tivo:' + mak + ' --insecure --url \"' + url + '\" | ';
        playerCommand += 'tivodecode -m ' + mak + ' --out ' + fp + '.mpg - | ';
        playerCommand += 'vlc --file-caching=2048 -';
        print('Fetching ' + url + '...');
        subprocess.Popen(playerCommand, stdout=sub.PIPE);


def resumeDownload(url, fp):
        video_file_size_start = chacheSize;
        block_size = 1024;
        video_file_size_current = 0;

        with open(fp, "a") as fd:
                for chunk in r.iter_content(block_size):
                        video_file_size_current += len(chunk);
                        if video_file_size_current >= video_file_size_start:
                                fd.write(chunk);
                fd.close();
        return;
于 2014-01-08T21:48:30.450 に答える