Guest User

Untitled

a guest
Mar 3rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'yaml'
  3. require 'fileutils'
  4. require 'mysql'
  5.  
  6. class Transcoder
  7. #Path to tools
  8. @@MENCODER = "/usr/local/bin/mencoder"
  9. @@MPLAYER = "/usr/local/bin/mplayer"
  10. @@FFMPEG = "/opt/local/bin/ffmpeg"
  11. @@ATOMICPARSLEY = '/opt/local/bin/AtomicParsley'
  12. @@LAME = '/opt/local/bin/lame'
  13.  
  14. # commands
  15. MENCODER_TO_MP4 = '%s "%s" -o "%s" -vf dsize=320:240:2,scale=-8:-8,harddup -oac faac -faacopts mpeg=4:object=2:raw:br=128 -channels 1 -srate 44100 -of lavf -lavfopts format=mp4 -ovc x264 -x264encopts bitrate=256:nocabac:level_idc=30:bframes=0:global_header:threads=auto:partitions=all -ofps 15'
  16. FFMPEG_TO_IMG = '%s -i "%s" -r 1 -t 00:00:01 -ss 00:20:00 -f image2 -y "%s"';
  17. ADD_MP4_METADATA = '%s "%s" --metaEnema --stik Movie --freefree --overWrite --artist "%s" --title "%s" --album "%s" --year "%s"--genre "%s" --comment "%s" --artwork "%s"'
  18. MPLAYER_TO_WAV = '%s -quiet -vo null -vc dummy -af volume=0,resample=44100:0:1 -ao pcm:waveheader:file="temp.wav" "%s"'
  19. LAME_TO_MP3 = '%s -V7 -h -B 56 --vbr-new --tt "%s" --ta "%s" --tl "%s" --ty "%s" --ti "%s" "temp.wav" "%s"'
  20.  
  21. VIDEO_ROOT = '/mnt/video/design/video/'
  22. GENRE = "UCLA Design | Media Arts"
  23.  
  24. @@LOCAL_ROOT = '/home/video/design/video/'
  25. @@DEBUG = true
  26.  
  27. @@MYSQL_HOST = 'localhost'
  28. @@MYSQL_USER = 'root'
  29. @@MYSQL_PASS = ''
  30.  
  31.  
  32. # Init and set config values
  33. def initialize
  34. if ENV['DMA_TRANSCODE_DEBUG']
  35. @@DEBUG = ENV['DMA_TRANSCODE_DEBUG'] == 'true'
  36. end
  37.  
  38. if ENV['DMA_TRANSCODE_LOCAL_ROOT']
  39. @@LOCAL_ROOT = ENV['DMA_TRANSCODE_LOCAL_ROOT']
  40. end
  41.  
  42. # set tool paths
  43. cmd = `which mencoder`.chomp!
  44. @@MENCODER = cmd if cmd != ''
  45.  
  46. cmd = `which mplayer`.chomp!
  47. @@MPLAYER = cmd if cmd != ''
  48.  
  49. cmd = `which ffmpeg`.chomp!
  50. @@FFMPEG = cmd if cmd != ''
  51.  
  52. cmd = `which AtomicParsley`.chomp!
  53. @@ATOMICPARSLEY = cmd if cmd != ''
  54.  
  55. cmd = `which lame`.chomp!
  56. @@LAME = cmd if cmd != ''
  57. end
  58.  
  59. def transcode(path, outpath, artist, title, album, year, genre, comment)
  60. # strip video root
  61. path = path.gsub(VIDEO_ROOT, @@LOCAL_ROOT)
  62.  
  63. # get the output paths
  64. mp4 = "%s/mp4/%s" % [outpath, path.gsub("rm", "mp4").gsub(@@LOCAL_ROOT, '')]
  65. mp3 = "%s/mp3/%s" % [outpath, path.gsub("rm", "mp3").gsub(@@LOCAL_ROOT, '')]
  66. img_format = "%s/img/%s" % [outpath, path.gsub(".rm", "_%d.jpg").gsub(@@LOCAL_ROOT, '')]
  67. img = "%s/img/%s" % [outpath, path.gsub("rm", "jpg").gsub(@@LOCAL_ROOT, '')]
  68.  
  69. # create the mp4
  70. if !@@DEBUG
  71. if File::exists?(path)
  72. print(MENCODER_TO_MP4 % [@@MENCODER, path, mp4])
  73. print "\n"
  74. system(MENCODER_TO_MP4 % [@@MENCODER, path, mp4])
  75. else
  76. print "------ ERROR ------\n COULD NOT FIND FILE #{path}.\n"
  77. return
  78. end
  79.  
  80. # create the thumbnail
  81. if File::exists?(mp4)
  82. print(FFMPEG_TO_IMG % [@@FFMPEG, mp4, img_format])
  83. print "\n"
  84.  
  85. system(FFMPEG_TO_IMG % [@@FFMPEG, mp4, img_format])
  86. File.rename(img_format % 1, img);
  87.  
  88. # add the metadata
  89. print(ADD_MP4_METADATA % [@@ATOMICPARSLEY, mp4, artist, title, album, year, genre, comment, img])
  90. print "\n"
  91.  
  92. system(ADD_MP4_METADATA % [@@ATOMICPARSLEY, mp4, artist, title, album, year, genre, comment, img])
  93. else
  94. print "------ ERROR ------\n COULD NOT FIND FILE #{mp4}.\n"
  95. return
  96. end
  97.  
  98. if File::exists?("temp.wav")
  99. File.delete("temp.wav")
  100. end
  101.  
  102. if File::exists?(path)
  103. print(MPLAYER_TO_WAV % [@@MPLAYER, path])
  104. print "\n"
  105.  
  106. system(MPLAYER_TO_WAV % [@@MPLAYER, path])
  107. else
  108. print "------ ERROR ------\n COULD NOT FIND FILE #{path}.\n"
  109. return
  110. end
  111.  
  112. if File::exists?("temp.wav")
  113. print(LAME_TO_MP3 % [@@LAME, title, artist, album, year, img, mp3])
  114. print "\n"
  115.  
  116. system(LAME_TO_MP3 % [@@LAME, title, artist, album, year, img, mp3])
  117. else
  118. print "------ ERROR ------\n COULD NOT FIND FILE temp.wav (MPLAYER must've failed).\n"
  119. return
  120. end
  121.  
  122. if File::exists?("temp.wav")
  123. File.delete("temp.wav")
  124. end
  125. end
  126.  
  127. print "%s\n%s\n%s\n" % [mp4,mp3,img]
  128. print " artist: %s\n title: %s\n album: %s\n year: %s\n genre: %s\ncomment: %s\n\n" % [artist, title, album, year, genre, comment]
  129.  
  130. end
  131.  
  132.  
  133. def process_from_yaml(srcyaml, outpath)
  134. f = open(srcyaml)
  135. data = YAML.load(f)
  136. f.close()
  137. process_array(data, outpath)
  138.  
  139. end
  140.  
  141.  
  142. def process_array(data, outpath)
  143. # get output dir names
  144. mp4_path = "%s/mp4/" % outpath;
  145. mp3_path = "%s/mp3/" % outpath;
  146. img_path = "%s/img/" % outpath;
  147.  
  148. # make output dirs
  149. FileUtils.mkdir_p(mp4_path)
  150. FileUtils.mkdir_p(mp3_path)
  151. FileUtils.mkdir_p(img_path)
  152.  
  153.  
  154. data['Video'].each do |k, video|
  155. transcode(video['stream_link'],
  156. outpath,
  157. artist=video['author'],
  158. title=video['title'],
  159. album=video['series_name'],
  160. year=DateTime.strptime(video['date_filmed'], "%Y-%m-%d %H:%M:%S").year,
  161. genre=GENRE,
  162. comment=video['description'])
  163. end
  164.  
  165. end
  166.  
  167.  
  168. def mysql_to_yaml
  169. db = Mysql::new(@@MYSQL_HOST, @@MYSQL_USER, @@MYSQL_PASS, 'dma')
  170.  
  171. video_root = /^\/mnt\/video\/design\/video\//
  172. videos = {"Video" => {}}
  173. python_data = {"Video" => {}}
  174. skipped = {"Video" => {}}
  175.  
  176. count = 1
  177. skipped_count = 1
  178. res = db.query('select * from dma_events where type="Lecture"')
  179.  
  180. res.each_hash do |row|
  181. temp = {}
  182.  
  183. temp['author'] = row['subtitle']
  184. temp['title'] = row['title']
  185. temp['biography'] = ""
  186. temp['description'] = row['longDescription']
  187. temp['date_filmed'] = row['startDate']
  188. temp['video_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".mp4")
  189. temp['dl_url'] = temp['video_url']
  190. temp['audio_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".mp3")
  191. temp['image_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".jpg")
  192. temp['is_public'] = true
  193.  
  194.  
  195. if video_root.match(row['streamLink'])
  196. videos['Video']["v#{count}"] = temp
  197.  
  198. pytemp = temp.clone;
  199. pytemp.delete('biography');
  200. pytemp.delete('video_url');
  201. pytemp.delete('dl_url');
  202. pytemp.delete('audio_url');
  203. pytemp.delete('image_url');
  204. pytemp.delete('is_public');
  205.  
  206. pytemp['stream_link'] = row['streamLink']
  207. pytemp['series_name'] = "UCLA Design | Media Arts"
  208. python_data['Video']["v#{count}"] = pytemp
  209.  
  210. count += 1
  211. else
  212. skipped['Video']["v#{skipped_count}"] = temp
  213. skipped_count += 1
  214. end
  215. end
  216.  
  217. if (true)
  218. # save fixture
  219. File.open( "dma_video_php.yml", "w" ) do |the_file|
  220. the_file.puts YAML::dump(videos)
  221. end
  222.  
  223. # save python data
  224. File.open( "dma_video_full.yml", "w" ) do |the_file|
  225. the_file.puts YAML::dump(python_data)
  226. end
  227.  
  228. # save log of skipped videos
  229. File.open( "dma_video_skipped.yml", "w" ) do |the_file|
  230. the_file.puts YAML::dump(skipped)
  231. end
  232.  
  233.  
  234. else
  235. print YAML::dump(videos)
  236. end
  237.  
  238. print count;
  239. end
  240.  
  241.  
  242.  
  243.  
  244. def process_ids(ids, outpath)
  245. db = Mysql::new(@@MYSQL_HOST, @@MYSQL_USER, @@MYSQL_PASS, 'dma')
  246.  
  247. video_root = /^\/mnt\/video\/design\/video\//
  248. videos = {"Video" => {}}
  249. python_data = {"Video" => {}}
  250.  
  251. count = 1
  252. skipped_count = 1
  253. res = db.query("select * from dma_events where id IN (#{ids})")
  254.  
  255. res.each_hash do |row|
  256. temp = {}
  257.  
  258. temp['author'] = row['subtitle']
  259. temp['title'] = row['title']
  260. temp['biography'] = ""
  261. temp['description'] = row['longDescription']
  262. temp['date_filmed'] = row['startDate']
  263. temp['video_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".mp4")
  264. temp['dl_url'] = temp['video_url']
  265. temp['audio_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".mp3")
  266. temp['image_url'] = row['streamLink'].gsub(video_root, "").gsub(/\.rm/, ".jpg")
  267. temp['is_public'] = true
  268.  
  269.  
  270. if video_root.match(row['streamLink'])
  271. videos['Video']["v#{count}"] = temp
  272.  
  273. pytemp = temp.clone;
  274. pytemp.delete('biography');
  275. pytemp.delete('video_url');
  276. pytemp.delete('dl_url');
  277. pytemp.delete('audio_url');
  278. pytemp.delete('image_url');
  279. pytemp.delete('is_public');
  280.  
  281. pytemp['stream_link'] = row['streamLink']
  282. pytemp['series_name'] = "UCLA Design | Media Arts"
  283. python_data['Video']["v#{count}"] = pytemp
  284.  
  285. count += 1
  286. end
  287. end
  288.  
  289. # save fixture
  290. File.open( "dma_video_php.yml", "w" ) do |the_file|
  291. the_file.puts YAML::dump(videos)
  292. end
  293.  
  294. process_array(python_data, outpath)
  295.  
  296. end
  297.  
  298.  
  299. # HELPERS TO GET MYSQL CONFIG FROM THE USER
  300. def ask(prompt, default)
  301. loop do
  302. print prompt, ' '
  303. $stdout.flush
  304. s = STDIN.gets
  305. exit if s == nil
  306. s.chomp!
  307. if s != ''
  308. return s
  309. elsif s == ''
  310. return default
  311. end
  312. end
  313. end
  314.  
  315. def ask_for_config
  316. @@MYSQL_HOST = ask("Please enter MySQL host [localhost]:", 'localhost')
  317. @@MYSQL_USER = ask("Please enter MySQL user [root]:", 'root')
  318. @@MYSQL_PASS = ask("Please enter MySQL pass []:", '')
  319. end
  320.  
  321.  
  322. end
  323.  
  324.  
  325. # ===========================================================
  326. # - Execute based on command line args
  327. # ===========================================================
  328. print "Welcome to the video.dma transcoder\n"
  329.  
  330. t = Transcoder.new
  331.  
  332. if !ARGV.empty?
  333. case ARGV[0]
  334. when 'gen_yaml'
  335. t.ask_for_config
  336. t.mysql_to_yaml
  337.  
  338. when 'process'
  339. t.process_from_yaml(ARGV[1], ARGV[2], ARGV[3])
  340.  
  341. when 'process_ids'
  342. t.ask_for_config
  343. t.process_ids(ARGV[1], ARGV[2])
  344. end
  345.  
  346. end
Add Comment
Please, Sign In to add comment