-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
executable file
·87 lines (73 loc) · 2.55 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# encoding=utf-8
import time # sleep
import sys # exit
import argparse # argparse
__author__ = 'Jack B. Du'
__copyright__ = 'Copyright (c) 2017, Jack B. Du'
__license__ = 'MIT'
__email__ = '[email protected]'
# parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument('filename', metavar='filename', type=str, help="path to file")
parser.add_argument('-fps', "--framerate", type=int, help="specify the frames per second")
args = parser.parse_args()
# buffer that stores all the frames of texts
framesBuffer = []
# load frames from the file
def load_frames(filename):
# initialize empty list for frames to return
frames = []
try:
# open file for reading
manvidfile = open(filename, 'r')
# error when opening file
except IOError:
print 'no such file or directory: ' + filename
sys.exit()
try:
# read first line in file
line = manvidfile.readline()
# get meta data from the file (first line in the file, seperated by ','
filetype, frameWidthStr, frameHeightStr, framerateStr, frameDepthStr = line.split(',')
except ValueError:
print 'error reading meta data: ' + filename
sys.exit()
if (filetype != 'manvid'):
print 'file type not supported: ' + filename
# while file not finished
while line != '':
# initialize an empty list for storing frame
frame = ''
# load one frame
for i in range(int(frameHeightStr)):
line = manvidfile.readline()
# add current line to frame
frame += line
# add current frame to frames
frames.append(frame)
# close the file
manvidfile.close()
return frames, int(frameWidthStr), int(frameHeightStr), int(framerateStr), int(frameDepthStr)
framesBuffer, frameWidth, frameHeight, framerate, frameDepth = load_frames(args.filename)
# change framerate if specified
if args.framerate != None:
framerate = args.framerate
# get the length (number) of frames
framesLength = len(framesBuffer)
try:
# repeat frames
while True:
# play frames
for i in range(framesLength):
# clear the terminal window
print(chr(27) + '[2J')
# print the frame
print framesBuffer[i]
# progress bar
print frameWidth*i/framesLength*'田'+(frameWidth-frameWidth*i/framesLength)*'囗'
# time for one frame
time.sleep(1.0/framerate)
# handle KeyboardInterrupt, typically Ctrl + C
except KeyboardInterrupt:
sys.exit()