wf-rec.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import sys, os, time, subprocess, signal
  4. import gi
  5. from gi.repository import GLib
  6. TMP_FILE = "/tmp/wf_rec_waybar"
  7. def get_time(s_time):
  8. n_time = time.time()
  9. return time.strftime("%H:%M:%S",time.gmtime(n_time - float(s_time)))
  10. def finish_record():
  11. os.system("killall -s SIGINT wf-recorder")
  12. os.system("rm {}".format(TMP_FILE))
  13. def get_record_area():
  14. global ss_size
  15. ss_size = subprocess.Popen(["slurp"], stdout = subprocess.PIPE)
  16. ss_size.wait()
  17. ss_size = ss_size.communicate()[0]
  18. ss_size = ss_size.decode("utf-8","strict")
  19. ss_size = ss_size.replace("\n","")
  20. return ss_size
  21. def open_tmp_file_start_record(ss_size):
  22. directory = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_VIDEOS)
  23. if directory == None:
  24. directory = os.path.expanduser("~")
  25. directory = os.path.join(directory,time.strftime("%F_%H%M%S.mp4",time.localtime(time.time())))
  26. if ss_size:
  27. get = subprocess.Popen(["sh","-c",'wf-recorder -g "{}" -a -f "{}"'.format(ss_size,directory)])
  28. else:
  29. get = subprocess.Popen(["sh","-c",'wf-recorder -a -f "{}"'.format(directory)])
  30. f = open(TMP_FILE,"w")
  31. s_time = str(time.time())
  32. f.write("{}\n{}".format(s_time,directory))
  33. f.close()
  34. def read_tmp_file():
  35. """File Format
  36. start_time
  37. directory"""
  38. f = open(TMP_FILE,"r")
  39. read_data = f.readlines()
  40. f.close()
  41. return read_data
  42. if __name__ == '__main__':
  43. args = sys.argv
  44. if os.path.exists(TMP_FILE):
  45. read_data = read_tmp_file()
  46. else:
  47. read_data = False
  48. if "--click" in args or "--middle" in args:
  49. if read_data:
  50. finish_record()
  51. else:
  52. ss_size = False
  53. if "--middle" in args:
  54. ss_size = get_record_area()
  55. open_tmp_file_start_record(ss_size)
  56. else:
  57. if read_data:
  58. print("<span color=\"#fa2772\"> {}</span>".format(get_time(read_data[0][:-1])))
  59. else:
  60. print("")