main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import re
  3. import argparse
  4. import time
  5. import dataset
  6. import pymysql
  7. pymysql.install_as_MySQLdb()
  8. from yaml import parse
  9. import pysrt
  10. import shutil
  11. from datetime import datetime
  12. from PIL import Image,ImageDraw,ImageFont
  13. import gspread
  14. import pandas as pd
  15. import csv
  16. def download_YT(URL=None):
  17. if URL is None:
  18. return "No URL"
  19. else:
  20. os.remove("test.mp3")
  21. cmd = "youtube-dl -x --audio-format mp3 -o 'test.mp3' " + URL
  22. os.system(cmd)
  23. return "download success"
  24. def srt(sound_file="test.mp3"):
  25. cmd = "autosub -S zh-TW -D zh-TW " + sound_file
  26. os.system(cmd)
  27. sound_srt_file = sound_file.split('.')[0] + ".srt"
  28. return "to srt success"
  29. def srt_into_db(url,srt_file="test.srt"):
  30. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gnews?charset=utf8mb4')
  31. subs = pysrt.open(srt_file)
  32. dict_tmp ={'URL':"",'text':"",'start_time':"",'end_time':""}
  33. for context in subs:
  34. #print(context.start.minutes*60+context.start.seconds+ 0.001*context.start.milliseconds)
  35. dict_tmp['URL'] = url
  36. dict_tmp['text'] = context.text
  37. dict_tmp['start_time'] = context.start
  38. dict_tmp['end_time'] = context.end
  39. result = db['srt'].insert(dict(dict_tmp))
  40. if __name__ == '__main__':
  41. parser = argparse.ArgumentParser(description='manual to this script')
  42. parser.add_argument('--url',type=str,default=None)
  43. args = parser.parse_args()
  44. URL = args.url
  45. #print(download_YT(URL))
  46. #print(srt())
  47. srt_into_db(URL)
  48. print("finish")