bagel 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.prettierignore +2 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +13 -0
  6. data/CHANGELOG.md +5 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +187 -0
  10. data/Rakefile +12 -0
  11. data/bagel.gemspec +32 -0
  12. data/bin/console +15 -0
  13. data/bin/setup +8 -0
  14. data/lib/bagel/colors.rb +8 -0
  15. data/lib/bagel/fonts.rb +3 -0
  16. data/lib/bagel/graphic/comment.rb +49 -0
  17. data/lib/bagel/graphic/intro.rb +49 -0
  18. data/lib/bagel/graphic/scoreboard/context.rb +28 -0
  19. data/lib/bagel/graphic/scoreboard/games.rb +13 -0
  20. data/lib/bagel/graphic/scoreboard/name.rb +32 -0
  21. data/lib/bagel/graphic/scoreboard/points.rb +13 -0
  22. data/lib/bagel/graphic/scoreboard/score.rb +38 -0
  23. data/lib/bagel/graphic/scoreboard/server.rb +16 -0
  24. data/lib/bagel/graphic/scoreboard/sets.rb +13 -0
  25. data/lib/bagel/graphic/scoreboard.rb +102 -0
  26. data/lib/bagel/graphic/stats/duration.rb +33 -0
  27. data/lib/bagel/graphic/stats/header.rb +32 -0
  28. data/lib/bagel/graphic/stats/result.rb +41 -0
  29. data/lib/bagel/graphic/stats/stat.rb +48 -0
  30. data/lib/bagel/graphic/stats.rb +80 -0
  31. data/lib/bagel/graphic.rb +23 -0
  32. data/lib/bagel/tennis/checker.rb +100 -0
  33. data/lib/bagel/tennis/outcome.rb +48 -0
  34. data/lib/bagel/tennis/player.rb +22 -0
  35. data/lib/bagel/tennis/point.rb +17 -0
  36. data/lib/bagel/tennis/score.rb +68 -0
  37. data/lib/bagel/tennis/scoring.rb +26 -0
  38. data/lib/bagel/tennis/serve.rb +36 -0
  39. data/lib/bagel/tennis/stats/aces.rb +28 -0
  40. data/lib/bagel/tennis/stats/break_points_won.rb +33 -0
  41. data/lib/bagel/tennis/stats/double_faults.rb +28 -0
  42. data/lib/bagel/tennis/stats/first_serve_in.rb +33 -0
  43. data/lib/bagel/tennis/stats/first_serve_points_won.rb +33 -0
  44. data/lib/bagel/tennis/stats/net_points_won.rb +33 -0
  45. data/lib/bagel/tennis/stats/points_won.rb +24 -0
  46. data/lib/bagel/tennis/stats/second_serve_points_won.rb +33 -0
  47. data/lib/bagel/tennis/stats/stat.rb +40 -0
  48. data/lib/bagel/tennis/stats/unforced_errors.rb +24 -0
  49. data/lib/bagel/tennis/stats/winners.rb +24 -0
  50. data/lib/bagel/tennis/stats.rb +16 -0
  51. data/lib/bagel/tennis.rb +13 -0
  52. data/lib/bagel/timeframe.rb +31 -0
  53. data/lib/bagel/version.rb +5 -0
  54. data/lib/bagel/video/clip.rb +41 -0
  55. data/lib/bagel/video/clip_builder_factory.rb +13 -0
  56. data/lib/bagel/video/clip_builders/clip_builder.rb +32 -0
  57. data/lib/bagel/video/clip_builders/intro_clip_builder.rb +37 -0
  58. data/lib/bagel/video/clip_builders/point_clip_builder.rb +67 -0
  59. data/lib/bagel/video/clip_builders/stats_clip_builder.rb +56 -0
  60. data/lib/bagel/video/clip_data_parser.rb +39 -0
  61. data/lib/bagel/video/clip_director.rb +16 -0
  62. data/lib/bagel/video/clip_saver.rb +59 -0
  63. data/lib/bagel/video/fade.rb +24 -0
  64. data/lib/bagel/video/ffmpeg/concat_file.rb +45 -0
  65. data/lib/bagel/video/ffmpeg/fade_filter.rb +34 -0
  66. data/lib/bagel/video/ffmpeg/overlay_filter.rb +52 -0
  67. data/lib/bagel/video/ffmpeg.rb +41 -0
  68. data/lib/bagel/video/orientation.rb +8 -0
  69. data/lib/bagel/video/overlays/comment.rb +15 -0
  70. data/lib/bagel/video/overlays/intro.rb +15 -0
  71. data/lib/bagel/video/overlays/overlay.rb +28 -0
  72. data/lib/bagel/video/overlays/scoreboard.rb +15 -0
  73. data/lib/bagel/video/overlays/second_serve.rb +15 -0
  74. data/lib/bagel/video/overlays/stats.rb +15 -0
  75. data/lib/bagel/video/padding.rb +9 -0
  76. data/lib/bagel/video/video.rb +18 -0
  77. data/lib/bagel/video.rb +32 -0
  78. data/lib/bagel.rb +76 -0
  79. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6d4b6c3ea3546c110bb0c0f47c44a66e3f01a0c1b3b4cca7c131766c5dbc0fd
4
+ data.tar.gz: 81877528261e49c263ace7d4024e10235ca383272c60b17ee9373dea39b80f67
5
+ SHA512:
6
+ metadata.gz: e52838783e9b7e56898882d6a26d7180763a61d00b21118ac6c5b4de1713b44497412dc5f9c069f96a504ca0245e0305eb3fba3f19b3645a572dada14c831377
7
+ data.tar.gz: a62bf13d31fed60a02df6c7cc7420a79a704fb3b2f58bd54340b2a3341917416d590ccdfb7c5ff555a86d47d060c1b11d1cc53deb0afb570a1bc9eb6bae8c34c
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.prettierignore ADDED
@@ -0,0 +1,2 @@
1
+ *.yml
2
+ *.yaml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-07-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Brent Chuang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # Bagel
2
+
3
+ ## Table of Contents
4
+ - [Introduction](#introduction)
5
+ - [Features](#features)
6
+ - [Dependencies](#dependencies)
7
+ - [Installation](#installation)
8
+ - [Usage](#usage)
9
+ - [Example YAML File](#example-yaml-file)
10
+ - [Configure and Start](#configure-and-start)
11
+ - [Sample Output](#sample-output)
12
+ - [Testing](#testing)
13
+ - [License](#license)
14
+ - [What Did I Use to Record My Videos?](#what-did-i-use-to-record-my-videos)
15
+ - [TODOs](#todos)
16
+
17
+ ## Introduction
18
+ Bagel is a tennis video editor/annotator.
19
+
20
+ ## Features
21
+
22
+ - Trim and concatenate videos
23
+ - Generate and overlay scoreboard/stat graphics
24
+
25
+ ## Dependencies
26
+
27
+ - [FFmpeg][ffmpeg]
28
+ - [ImageMagick][imagemagick]
29
+
30
+ ## Installation
31
+
32
+ Install native dependencies using [Homebrew][homebrew]:
33
+ ```
34
+ $ brew install ffmpeg pkg-config imagemagick
35
+ ```
36
+
37
+ Add this line to your application's Gemfile:
38
+ ```ruby
39
+ gem 'bagel'
40
+ ```
41
+ And then execute:
42
+ ```
43
+ $ bundle install
44
+ ```
45
+ Or install it yourself as:
46
+ ```ruby
47
+ $ gem install bagel
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ 1. Make sure your source video is a single file. If your camera splits the recorded video into smaller chunks like mine does, concatenate them first:
53
+
54
+ ```ruby
55
+ require 'bagel'
56
+
57
+ Bagel::Video.::FFMPEG.concat(
58
+ source: 'path/to/chunks',
59
+ destination: 'path/to/output.mp4',
60
+ transcode: false
61
+ )
62
+ ```
63
+
64
+ 2. Chart the match and build the "clips file" using YAML. This file will be used to build the final video.
65
+
66
+ Every clip must specify:
67
+ - `type`
68
+ - has to be either `intro`, `point`, or `stats`
69
+ - `start` and `finish` timestamps
70
+ - which part of the source video to trim (e.g. `'01:23'`)
71
+
72
+ `intro` clips must also specify:
73
+ - `title` and `description`
74
+ - e.g. the location and date/time of when the match was played
75
+
76
+ `point` clips must also specify:
77
+ - `server`
78
+ - the player who served the point (`1` or `2`)
79
+ - `score`
80
+ - a nested array representing the current score AT THE TIME OF SERVE
81
+ - e.g. 6-4, 2-0, 40-15 is represented as [[[6, 4], [2, 0]], [3, 1]])
82
+ - `faults`
83
+ - number of faults during serve
84
+ - `winner`
85
+ - the player who won the point (`1` or`2`)
86
+ - `reason`
87
+ - the circumstance under which the point was won
88
+ - has to be either `W` for winner, `UE` for unforced error, or `FE` for forced error
89
+ - `shot`
90
+ - the type of shot that ended the point
91
+ - has to be either `S` for serve, `FH` for forehand, `BH` for backhand, or `OH` for overhead.
92
+ - `net` (optional)
93
+ - an array containing details about whether the point was a net point for either player and the subsequent outcome
94
+ - `true` = won the net point, `false` = lost the net point, `null` = player did not play a net point
95
+ - e.g. [ true, null ] = player 1 won a net point, player 2 was not at the net
96
+ - e.g. [ null, false ] = player 1 was not at the net, player 2 lost a net point (volley error etc.)
97
+
98
+ `stats` clips must also specify:
99
+ - `score`
100
+ - a nested array representing the current score AT THE END OF THE SET
101
+ - e.g. 6-4, 2-6 is represented as [[[6, 4], [2, 6]]]
102
+
103
+ ### Example YAML File
104
+
105
+ ```yaml
106
+ # Intro
107
+ - { type: intro, start: '00:00', finish: '00:08', title: 'My Tennis Club', description: 'May 16, 2021' }
108
+
109
+ # Set 1 points
110
+ - { type: point, server: 1, score: [[[0, 0]], [0, 0]], start: '02:02', faults: 1, finish: '02:07', winner: 1, reason: UE, shot: BH }
111
+ # ...
112
+ - { type: point, server: 2, score: [[[5, 4]], [3, 2]], start: '37:53', faults: 1, finish: '38:03', winner: 1, reason: W, shot: FH, net: [ true, false ] }
113
+
114
+ # Set 1 stats
115
+ - { type: stats, start: '38:35', finish: '38:47', score: [[[6, 4]]] }
116
+
117
+ # Set 2 points
118
+ - { type: point, server: 1, score: [[[6, 4], [0, 0]], [0, 0]], start: '40:41', faults: 1, finish: '40:55', winner: 1, reason: W, shot: BH, net: [ true, false ] }
119
+ # ...
120
+ - { type: point, server: 2, score: [[[6, 4], [2, 5]], [1, 3]], start: '1:14:05', faults: 0, finish: '1:14:11', winner: 2, reason: UE, shot: FH }
121
+
122
+ # Set 2 stats
123
+ - { type: stats, start: '1:14:24', finish: '1:14:36', score: [[[6, 4], [2, 6]]] }
124
+ ```
125
+
126
+ ### Configure and Start
127
+
128
+ ```ruby
129
+ require 'bagel'
130
+
131
+ Bagel.configure do |config|
132
+ config.name = '2021-05-16' # this will be used to namespace the temp clip/graphic files
133
+ config.source_video = 'path/to/source/video.mp4'
134
+ config.clip_data = 'path/to/data/file.yml'
135
+ config.destination_video = 'path/to/destination/video.mp4'
136
+ config.names = %w(Alice Bob)
137
+ end
138
+
139
+ Bagel.make_video
140
+ ```
141
+
142
+ ## Sample Output
143
+
144
+ ![intro]
145
+ ![point-1]
146
+ ![point-2]
147
+ ![stats]
148
+
149
+ ## Testing
150
+
151
+ ```
152
+ $ rspec
153
+ ```
154
+ Tested on
155
+ - Ruby 3.0.1
156
+ - ffmpeg 4.4
157
+ - ImageMagick 7.0.11-12
158
+
159
+ ## License
160
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
161
+
162
+ ## Useful Links
163
+ - https://stackoverflow.com/questions/57045900/how-to-find-a-safe-point-for-ss-using-ffmpeg-to-avoid-breaking-a-v-sync
164
+ - https://stackoverflow.com/questions/42943805/ffmpeg-is-it-possible-to-overlay-multiple-images-over-a-video-at-specified-inte
165
+ - https://trac.ffmpeg.org/wiki/Seeking#Seekingwhiledoingacodeccopy
166
+ - https://itectec.com/superuser/video-cut-with-missing-frames-in-ffmpeg/
167
+ - http://www.markbuckler.com/post/cutting-ffmpeg/
168
+
169
+ ## What Did I Use to Record My Videos?
170
+ - [QM-1 Portable Camera Mount][qm-1]
171
+ - Sony Action Cam HDR-AS200V
172
+
173
+ # TODOs
174
+ - customizable audio/video quality
175
+ - customizable stats
176
+ - customizable scoreboards
177
+ - themed scoreboards (AO, RG, Wimbledon, USO, etc.)
178
+ - more details (deuce #, breakpoint #, etc.)
179
+
180
+ [ffmpeg]: https://www.ffmpeg.org
181
+ [imagemagick]: https://imagemagick.org
182
+ [homebrew]: https://brew.sh
183
+ [intro]: https://www.dropbox.com/s/64xc8xw15fxdton/intro.jpg?raw=1 "intro"
184
+ [point-1]: https://www.dropbox.com/s/ny6h5ja5d5doheg/point-1.jpg?raw=1 "point"
185
+ [point-2]: https://www.dropbox.com/s/gyvfkxx389y8pac/point-2.jpg?raw=1 "point"
186
+ [stats]: https://www.dropbox.com/s/gjm6bhshuchfwi9/stats.jpg?raw=1 "stats"
187
+ [qm-1]: https://mytennistools.com/play-smarter-with-the-qm-1
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bagel.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bagel/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bagel"
7
+ spec.version = Bagel::VERSION
8
+ spec.authors = ["Brent Chuang"]
9
+ spec.email = ["burennto@gmail.com"]
10
+
11
+ spec.summary = "Bagel is a tennis video editor/annotator."
12
+ spec.homepage = "https://github.com/burennto/bagel"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.4.0"
15
+
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "rmagick", "~> 4.2", ">= 4.2.2"
24
+
25
+ spec.add_development_dependency "rake", "~> 13.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rubocop", "~> 1.7"
28
+ spec.add_development_dependency "factory_bot", "~> 6.2"
29
+
30
+ # For more information and examples about making a new gem, checkout our
31
+ # guide at: https://bundler.io/guides/creating_gem.html
32
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bagel"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ COLOR_TRANSPARENT = 'transparent'
2
+ COLOR_WHITE = '#E0E0E0'
3
+ COLOR_BLACK = '#000000'
4
+ COLOR_GREY = '#303030'
5
+ COLOR_GREY_DARK = '#303030'
6
+ COLOR_GREY_LIGHT = '#404040'
7
+ COLOR_BLUE = '#3498DB'
8
+ COLOR_ORANGE = '#F39C12'
@@ -0,0 +1,3 @@
1
+ FONT_FAMILY = 'Helvetica'
2
+ FONT_WEIGHT = BoldWeight
3
+ FONT_SIZE = 32
@@ -0,0 +1,49 @@
1
+ module Bagel::Graphic
2
+ class Comment
3
+ HEIGHT = 40
4
+ PADDING = 10
5
+
6
+ FILENAME_PREFIX = 'comment-'
7
+
8
+ def initialize(text)
9
+ @text = text
10
+ end
11
+
12
+ def save
13
+ text = Image.new(1920, 1080) { self.background_color = COLOR_WHITE }
14
+
15
+ draw = Magick::Draw.new do |d|
16
+ d.font_family = FONT_FAMILY
17
+ d.font_weight = FONT_WEIGHT
18
+ d.pointsize = 24
19
+ d.gravity = CenterGravity
20
+ d.fill = COLOR_BLACK
21
+ end
22
+
23
+ draw.annotate(text, 0, 0, 0, 0, @text)
24
+ text.trim!
25
+
26
+ canvas = Image.new(text.columns + PADDING * 2, HEIGHT) { self.background_color = COLOR_WHITE }
27
+ canvas.composite!(text, CenterGravity, 0, 0, OverCompositeOp)
28
+
29
+ canvas.write(path)
30
+
31
+ path
32
+ end
33
+
34
+ private
35
+
36
+ def filename
37
+ name = @text.downcase
38
+ .gsub(/[^\w\s_-]+/, '')
39
+ .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
40
+ .gsub(/\s+/, '-')
41
+
42
+ FILENAME_PREFIX + name + FILENAME_EXTENSION
43
+ end
44
+
45
+ def path
46
+ File.join(Bagel.config.graphic_dir, filename)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ module Bagel::Graphic
2
+ class Intro
3
+ FILENAME = '00-intro.png'
4
+ MARGIN_X = 0
5
+ MARGIN_Y = 20
6
+
7
+ def initialize(title='', description='')
8
+ @title = title
9
+ @description = description
10
+ end
11
+
12
+ def save
13
+ top = Image.new(1920, 1080) { self.background_color = COLOR_TRANSPARENT }
14
+ draw.font_weight = BoldWeight
15
+ draw.pointsize = 64
16
+ draw.annotate(top, 0, 0, 0, 0, @title.upcase)
17
+ top.trim!
18
+
19
+ bottom = Image.new(1920, 1080) { self.background_color = COLOR_TRANSPARENT }
20
+ draw.font_weight = NormalWeight
21
+ draw.pointsize = 48
22
+ draw.annotate(bottom, 0, 0, 0, 0, @description)
23
+ bottom.trim!
24
+
25
+ height = top.rows + bottom.rows + MARGIN_Y
26
+ canvas = Image.new(1920, height) { self.background_color = COLOR_TRANSPARENT }
27
+ canvas.composite!(top, NorthWestGravity, MARGIN_X, 0, OverCompositeOp)
28
+ canvas.composite!(bottom, SouthWestGravity, MARGIN_X, 0, OverCompositeOp)
29
+ canvas.trim!
30
+ canvas.write(path)
31
+
32
+ path
33
+ end
34
+
35
+ private
36
+
37
+ def draw
38
+ @draw ||= Magick::Draw.new do |d|
39
+ d.font_family = FONT_FAMILY
40
+ d.gravity = WestGravity
41
+ d.fill = COLOR_WHITE
42
+ end
43
+ end
44
+
45
+ def path
46
+ File.join(Bagel.config.graphic_dir, FILENAME)
47
+ end
48
+ end
49
+ end