Collection of scripts to make automatic video highlights of varying quality
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

92 Zeilen
2.7 KiB

  1. #!/bin/python3
  2. """ basicclips.py -- generate video of clips from another video
  3. uses ffprobe, ffmpeg and MELT
  4. """
  5. DEFAULT_CLIPS = 9
  6. DEFAULT_DURATION = 15
  7. DEFAULT_MIXER_DURATION = 2
  8. FRAMERATE = 60
  9. def get_video_duration(videofile=None):
  10. """Get video duration in seconds using ffprobe"""
  11. import subprocess
  12. if videofile is not None:
  13. ffprobe_args = ["ffprobe", "-v", "error", "-show_entries",
  14. "format=duration", "-of",
  15. "default=noprint_wrappers=1:nokey=1",
  16. videofile]
  17. seconds = subprocess.run(ffprobe_args, capture_output=True)
  18. if seconds:
  19. try:
  20. seconds = int(float(seconds.stdout))
  21. return seconds
  22. except ValueError as e:
  23. print("Error getting seconds for ", videofile)
  24. print(e)
  25. if __name__ == "__main__":
  26. import sys
  27. import os
  28. import tempfile
  29. import subprocess
  30. # Check/set settings using env vars
  31. if os.environ.get("HIGHLIGHT_CLIPS"):
  32. clips = os.environ.get("HIGHLIGHT_CLIPS")
  33. else:
  34. clips = DEFAULT_CLIPS
  35. if os.environ.get("HIGHLIGHT_DURATION"):
  36. duration = os.environ.get("HIGHLIGHT_DURATION")
  37. else:
  38. duration = DEFAULT_DURATION
  39. # TODO: argv sanity checks
  40. videofile = sys.argv[1]
  41. outputfile = sys.argv[2]
  42. video_duration = get_video_duration(videofile)
  43. mixer_duration = FRAMERATE * DEFAULT_MIXER_DURATION
  44. ffmpeg_common_args = ["ffmpeg", "-hide_banner",
  45. "-loglevel", "quiet"]
  46. melt_args = []
  47. with tempfile.TemporaryDirectory() as tmpdir:
  48. tempclip_length = duration - 3
  49. for i in range(1, clips+1):
  50. print("Processing clip", i)
  51. seek = int(float(video_duration * i / float(clips+1)))
  52. if i == 1:
  53. # no intro mixer on first clip
  54. melt_args.extend(["melt",
  55. os.path.join(tmpdir, "hi{}.mkv".format(i))])
  56. else:
  57. melt_args.extend([os.path.join(tmpdir, "hi{}.mkv".format(i)),
  58. "-mix", str(mixer_duration),
  59. "-mixer", "luma"])
  60. ffmpeg_args = ffmpeg_common_args + [
  61. "-ss", str(seek),
  62. "-i", videofile, "-t", str(tempclip_length),
  63. "-c", "copy", os.path.join(tmpdir, "hi{}.mkv".format(i))
  64. ]
  65. print(ffmpeg_args)
  66. subprocess.run(ffmpeg_args)
  67. # MELT output format
  68. melt_args.extend(["-consumer", "avformat:{}".format(outputfile),
  69. "crf=20"])
  70. print(melt_args)
  71. subprocess.run(melt_args)
  72. print("Done!")