-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_trans.py
90 lines (66 loc) · 1.94 KB
/
random_trans.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
import os
import sys
import time
import random
import signal
import threading
import subprocess
ts_num = 0
path = sys.argv[1]
resolution = sys.argv[2]
all_files = os.listdir(path)
random.shuffle(all_files)
segment_num = 2
all_files = all_files[0:segment_num]
start_time = time.time()
class trans_thread(threading.Thread):
def __init__(self, lock, index):
threading.Thread.__init__(self)
self.lock = lock
self.index = index
def run(self):
global path
global ts_num
global resolution
global all_files
global start_time
while True:
self.lock.acquire()
try:
file_name = all_files.pop()
except:
self.lock.release()
break
self.lock.release()
input_file = os.path.join(path, file_name)
name, suffix = os.path.splitext(input_file)
if suffix != ".ts" or resolution in name:
continue
output_file = name + "_" + resolution + ".ts"
cmd = "ffmpeg -y -i " + input_file + " -s " + \
resolution + " " + output_file
print cmd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#for line in p.stdout.readlines():
# print line,
retval = p.wait()
self.lock.acquire()
ts_num = ts_num + 1
self.lock.release()
exec_time = time.time() - start_time
ave_speed = exec_time / ts_num
print "average speed is ", ave_speed
lock = threading.Lock()
thread_num = 9
threads = []
try:
for i in range(0, thread_num):
t = trans_thread(lock, i)
t.start()
threads.append(t)
except:
print "Error: unable to start thread"
for t in threads:
t.join()
total_time = time.time() - start_time
print 'total time:', total_time