Guest User

Untitled

a guest
Nov 14th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import os
  2. import sys
  3. import glob
  4. import subprocess
  5.  
  6. from pytube import YouTube
  7.  
  8.  
  9. if __name__=="__main__":
  10. if len(sys.argv) != 2:
  11. print("usage: this.py [test or train]")
  12. quit()
  13.  
  14. if sys.argv[1] == "test":
  15. mode = "test"
  16. elif sys.argv[1] == "train":
  17. mode = "train"
  18. else:
  19. print("invalid mode")
  20. quit()
  21.  
  22. data_root = "./RealEstate10K/" + mode
  23.  
  24. seqname_list = sorted(glob.glob(data_root + "/*.txt"))
  25. print("{} sequences are saved".format(len(seqname_list)))
  26.  
  27. for txt_file in seqname_list:
  28. print("{} is the current target.".format(txt_file))
  29.  
  30. dir_name = txt_file.split('/')[-1]
  31. dir_name = dir_name.split('.')[0]
  32. output_root = './videos/' + mode + '/' + dir_name
  33.  
  34. if not os.path.exists(output_root):
  35. os.makedirs(output_root)
  36. else:
  37. continue
  38.  
  39. seq_file = open(txt_file, "r")
  40. lines = seq_file.readlines()
  41. timestamp_list = []
  42. str_timestamp_list = []
  43. for idx, line in enumerate(lines):
  44. if idx == 0:
  45. youtube_url = line.strip()
  46. else:
  47. timestamp = int(line.split(' ')[0])
  48. str_timestamp_list.append(str(timestamp))
  49. timestamp = int(timestamp/1000)
  50. str_hour = str(int(timestamp/3600000)).zfill(2)
  51. str_min = str(int(int(timestamp%3600000)/60000)).zfill(2)
  52. str_sec = str(int(int(int(timestamp%3600000)%60000)/1000)).zfill(2)
  53. str_mill = str(int(int(int(timestamp%3600000)%60000)%1000)).zfill(3)
  54. str_timestamp = str_hour+":"+str_min+":"+str_sec+"."+str_mill
  55. timestamp_list.append(str_timestamp)
  56.  
  57. seq_file.close()
  58. try :
  59. yt = YouTube(youtube_url)
  60. stream = yt.streams.first()
  61. stream.download('./','current')
  62. except :
  63. failure_log = open('falied_videos.txt', 'a')
  64. failure_log.writelines(txt_file+'\n')
  65. failure_log.close()
  66. continue
  67.  
  68. videoname_candinate_list = glob.glob('./*')
  69. for videoname_candinate in videoname_candinate_list:
  70. print(videoname_candinate.split('.'))
  71. if videoname_candinate.split('.')[-2] == "/current":
  72. videoname = videoname_candinate
  73.  
  74. # ffmpeg -i tmp.mp4 -ss 00:01:28.800 -vframes 1 -f image2 out.jpg
  75. for idx, timestamp in enumerate(timestamp_list):
  76. command = 'ffmpeg'+' -ss '+timestamp+' -i '+videoname+' -vframes 1 -f image2 '+output_root+'/'+str_timestamp_list[idx]+'.png'
  77. os.system(command)
  78. command = "rm " + videoname
  79. os.system(command)
  80. print("done!")
Add Comment
Please, Sign In to add comment