Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. def __init__(self, kargs):
  2. self.params = kargs
  3.  
  4. # will store generated Transcode objects
  5. self.transcodes = []
  6.  
  7. # Queue to thread ffmpeg processes with
  8. self.q = Queue.Queue()
  9.  
  10. # generate transcodes with self.params for all rasters x formats
  11. self.generate_transcodes()
  12.  
  13. # give the Queue the right number of task threads to make
  14. for i in range(self.get_num_transcodes()):
  15. self.q.put(i)
  16.  
  17. for transcode in self.transcodes:
  18. transcode.print_commands()
  19.  
  20. # testing code to be sure command strings are generating appropriately
  21. for transcode in self.transcodes:
  22. self.q.put(transcode)
  23.  
  24. # kick off all transcodes by creating a new daemon (terminating on program close) thread;
  25. # the thread is given each Transcode's run() method, which is dynamically created within
  26. # the Transcode constructor given its command strings
  27. for transcode in self.transcodes:
  28. t = threading.Thread(target=transcode.run, args=(self.q.get, self.q.task_done))
  29. t.daemon = True
  30. t.start()
  31.  
  32. print("Transcoding in progress...")
  33.  
  34. # go through each transcode and print which process is currently underway, then sleep
  35. # 1 = first pass, 2 = second pass, 3 = complete
  36. while True:
  37. still_running = False
  38. for transcode in self.transcodes:
  39. if not transcode.complete:
  40. still_running = True
  41. print('Transcode %s still running!' % transcode.filename)
  42. if transcode.current_proc in range(3):
  43. print(os.path.basename(transcode.filename) + ': pass %s' % transcode.current_proc)
  44. else:
  45. print(os.path.basename(transcode.filename) + ': complete!')
  46. print(transcode.complete)
  47. if not still_running:
  48. break
  49. time.sleep(2)
  50. print('poll')
  51.  
  52. # block until all items in queue are gotten and processed
  53. print('About to join...')
  54. self.q.join()
  55.  
  56. print('End of transcode batch!')
  57.  
  58. '''
  59. Executes sequentially the command strings given to this Transcode via subprocess; it will
  60. first execute one, then the next, as the second command relies on the first being completed.
  61. It will take in a get() and done() method that are Queue methods and call them at the right
  62. moments to signify to an external Queue object that the worker thread needed for this instance
  63. is ready to start and finish.
  64.  
  65. @param get A Queue get() function to be called at run()'s start.
  66. @param done A Queue done() function to be called at run()'s end.
  67.  
  68. @return none
  69. '''
  70. def run(self, get, done):
  71. # get a thread from the queue
  72. get()
  73.  
  74. # convert command lists to command strings
  75. for i in range(len(self.commands)):
  76. self.commands[i] = ' '.join(self.commands[i])
  77.  
  78. # show that we're working with our first command string
  79. self.current_proc = 1
  80.  
  81. # assign our current proc the first command string subprocess
  82. self.proc = Popen(self.commands[0], stdout=PIPE, stderr=PIPE, shell=True)
  83. # execute process until complete
  84. self.proc.communicate()
  85.  
  86. print('Transcode %s first pass complete' % self.identifier)
  87.  
  88. # run second command string if exists
  89. if len(self.commands) > 1:
  90.  
  91. # show that we're working with second command string
  92. self.current_proc = 2
  93.  
  94. # spawn second process and parse output line by line as before
  95. self.proc = Popen(self.commands[1], stdout=PIPE, stderr=PIPE, shell=True)
  96. # execute process until complete
  97. self.proc.communicate()
  98.  
  99. print('Transcode %s second pass complete' % self.identifier)
  100.  
  101. # delete log files when done
  102. if self.logfile and os.path.exists(self.logfile + '-0.log'):
  103. os.remove(self.logfile + '-0.log')
  104. if self.logfile and os.path.exists(self.logfile + '-0.log.mbtree'):
  105. os.remove(self.logfile + '-0.log.mbtree')
  106. if self.logfile and os.path.exists(self.logfile + '-0.log.temp'):
  107. os.remove(self.logfile + '-0.log.temp')
  108. if self.logfile and os.path.exists(self.logfile + '-0.log.mbtree.temp'):
  109. os.remove(self.logfile + '-0.log.mbtree.temp')
  110.  
  111. self.complete = True
  112. print('Transcode %s complete' % self.identifier)
  113.  
  114. # assign value of 3 to signify completed task
  115. self.current_proc = 3
  116.  
  117. # finish up with Queue task
  118. done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement