distorted 0.6.0 → 0.7.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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bin/console +14 -0
- data/bin/distorted +6 -0
- data/bin/setup +8 -0
- data/lib/distorted.rb +2 -0
- data/lib/distorted/checking_you_out.rb +116 -13
- data/lib/distorted/{types → checking_you_out}/README +0 -0
- data/lib/distorted/checking_you_out/application.yaml +33 -0
- data/lib/distorted/{types → checking_you_out}/font.yaml +0 -0
- data/lib/distorted/checking_you_out/image.yaml +108 -0
- data/lib/distorted/click_again.rb +333 -0
- data/lib/distorted/element_of_media.rb +2 -0
- data/lib/distorted/element_of_media/change.rb +119 -0
- data/lib/distorted/element_of_media/compound.rb +120 -0
- data/lib/distorted/floor.rb +17 -0
- data/lib/distorted/invoker.rb +97 -0
- data/lib/distorted/media_molecule.rb +58 -0
- data/lib/distorted/media_molecule/font.rb +195 -0
- data/lib/distorted/media_molecule/image.rb +33 -0
- data/lib/distorted/media_molecule/pdf.rb +44 -0
- data/lib/distorted/media_molecule/svg.rb +45 -0
- data/lib/distorted/media_molecule/text.rb +203 -0
- data/lib/distorted/media_molecule/video.rb +18 -0
- data/lib/distorted/modular_technology/gstreamer.rb +174 -0
- data/lib/distorted/modular_technology/vips.rb +4 -4
- data/lib/distorted/modular_technology/vips/foreign.rb +489 -0
- data/lib/distorted/modular_technology/vips/load.rb +133 -0
- data/lib/distorted/modular_technology/{vips_save.rb → vips/save.rb} +23 -34
- data/lib/distorted/monkey_business/encoding.rb +317 -0
- data/lib/distorted/monkey_business/hash.rb +0 -15
- data/lib/distorted/{modular_technology/triple_counter.rb → triple_counter.rb} +8 -1
- data/lib/distorted/version.rb +16 -16
- metadata +59 -46
- data/lib/distorted/injection_of_love.rb +0 -247
- data/lib/distorted/modular_technology/vips_load.rb +0 -77
- data/lib/distorted/molecule/C18H27NO3.rb +0 -10
- data/lib/distorted/molecule/font.rb +0 -198
- data/lib/distorted/molecule/image.rb +0 -36
- data/lib/distorted/molecule/pdf.rb +0 -119
- data/lib/distorted/molecule/svg.rb +0 -60
- data/lib/distorted/molecule/text.rb +0 -225
- data/lib/distorted/molecule/video.rb +0 -195
- data/lib/distorted/monkey_business/mnemoniq.rb +0 -8
- data/lib/distorted/types/application.yaml +0 -8
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require 'distorted/checking_you_out'
|
5
|
+
require 'distorted/modular_technology/vips'
|
6
|
+
|
7
|
+
|
8
|
+
module Cooltrainer; end
|
9
|
+
module Cooltrainer::DistorteD; end
|
10
|
+
module Cooltrainer::DistorteD::Molecule; end
|
11
|
+
module Cooltrainer::DistorteD::Molecule::Image
|
12
|
+
|
13
|
+
|
14
|
+
# Attributes for our <picture>/<img>.
|
15
|
+
# Automatically enabled as attrs for DD Liquid Tag.
|
16
|
+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture#Attributes
|
17
|
+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#Attributes
|
18
|
+
# https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading
|
19
|
+
ATTRIBUTES = Set[:alt, :caption, :href, :loading]
|
20
|
+
|
21
|
+
# Defaults for HTML Element attributes.
|
22
|
+
# Not every attr has to be listed here.
|
23
|
+
# Many need no default and just won't render.
|
24
|
+
ATTRIBUTES_DEFAULT = {
|
25
|
+
:loading => :eager,
|
26
|
+
}
|
27
|
+
ATTRIBUTES_VALUES = {
|
28
|
+
:loading => Set[:eager, :lazy],
|
29
|
+
}
|
30
|
+
|
31
|
+
include Cooltrainer::DistorteD::Technology::Vips
|
32
|
+
|
33
|
+
end # Image
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'hexapdf'
|
4
|
+
|
5
|
+
require 'distorted/checking_you_out'
|
6
|
+
|
7
|
+
|
8
|
+
module Cooltrainer; end
|
9
|
+
module Cooltrainer::DistorteD; end
|
10
|
+
module Cooltrainer::DistorteD::Molecule; end
|
11
|
+
module Cooltrainer::DistorteD::Molecule::PDF
|
12
|
+
|
13
|
+
|
14
|
+
# https://hexapdf.gettalong.org/documentation/reference/api/HexaPDF/Document/index.html#method-c-new
|
15
|
+
# https://hexapdf.gettalong.org/documentation/reference/api/HexaPDF/index.html#DefaultDocumentConfiguration
|
16
|
+
# https://hexapdf.gettalong.org/documentation/reference/api/HexaPDF/Task/Optimize.html
|
17
|
+
PDF_TYPE = CHECKING::YOU::OUT['application/pdf']
|
18
|
+
LOWER_WORLD = Hash[
|
19
|
+
PDF_TYPE => nil,
|
20
|
+
]
|
21
|
+
OUTER_LIMITS = Hash[
|
22
|
+
PDF_TYPE => nil,
|
23
|
+
]
|
24
|
+
|
25
|
+
# TODO: Use MuPDF instead of libvips magick-based PDF loader.
|
26
|
+
|
27
|
+
def self.optimize(src, dest)
|
28
|
+
HexaPDF::Document.open(src) do |doc|
|
29
|
+
doc.task(
|
30
|
+
:optimize,
|
31
|
+
compact: true,
|
32
|
+
object_streams: :generate,
|
33
|
+
xref_streams: :generate,
|
34
|
+
compress_pages: false,
|
35
|
+
)
|
36
|
+
doc.write(dest)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method(PDF_TYPE.distorted_file_method) { |dest_root, change|
|
41
|
+
copy_file(change.paths(dest_root).first)
|
42
|
+
}
|
43
|
+
|
44
|
+
end # PDF
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'svg_optimizer' # https://github.com/fnando/svg_optimizer
|
4
|
+
|
5
|
+
require 'distorted/checking_you_out'
|
6
|
+
require 'distorted/modular_technology/vips/save'
|
7
|
+
|
8
|
+
|
9
|
+
module Cooltrainer; end
|
10
|
+
module Cooltrainer::DistorteD; end
|
11
|
+
module Cooltrainer::DistorteD::Molecule; end
|
12
|
+
module Cooltrainer::DistorteD::Molecule::SVG
|
13
|
+
|
14
|
+
include Cooltrainer::DistorteD::Technology::Vips::Save
|
15
|
+
|
16
|
+
LOWER_WORLD = Hash[
|
17
|
+
CHECKING::YOU::OUT['image/svg+xml'] => Cooltrainer::DistorteD::Technology::Vips::vips_get_options(
|
18
|
+
Cooltrainer::DistorteD::Technology::Vips::vips_foreign_find_load_suffix(".#{CHECKING::YOU::OUT['image/svg+xml'].preferred_extension}")
|
19
|
+
).merge(Hash[
|
20
|
+
:optimize => Cooltrainer::Compound.new(:optimize, valid: Cooltrainer::BOOLEAN_VALUES, default: false, blurb: 'SvgOptimizer'),
|
21
|
+
])
|
22
|
+
]
|
23
|
+
|
24
|
+
# WISHLIST: Support VML for old IE compatibility.
|
25
|
+
# Example: RaphaëlJS — https://en.wikipedia.org/wiki/Rapha%C3%ABl_(JavaScript_library)
|
26
|
+
OUTER_LIMITS = Hash[
|
27
|
+
CHECKING::YOU::OUT['image/svg+xml'] => nil,
|
28
|
+
]
|
29
|
+
|
30
|
+
def to_vips_image
|
31
|
+
# NOTE: libvips 8.9 added the `unlimited` argument to svgload.
|
32
|
+
# Loading SVGs >= 10MiB in size will fail on older libvips.
|
33
|
+
# https://github.com/libvips/libvips/commit/55e49831b801e05ddd974b1e2102fda7956c53f5
|
34
|
+
@vips_image ||= Vips::Image.new_from_file(path)
|
35
|
+
end
|
36
|
+
|
37
|
+
define_method(CHECKING::YOU::OUT['image/svg+xml'].distorted_file_method) { |dest_root, change|
|
38
|
+
if change.optimize
|
39
|
+
SvgOptimizer.optimize_file(path, change.paths(dest_root).first, SvgOptimizer::DEFAULT_PLUGINS)
|
40
|
+
else
|
41
|
+
copy_file(change.paths(dest_root).first)
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'charlock_holmes' # Text file charset detection
|
4
|
+
|
5
|
+
require 'distorted/monkey_business/encoding'
|
6
|
+
require 'distorted/monkey_business/string' # String#map
|
7
|
+
require 'distorted/modular_technology/pango'
|
8
|
+
require 'distorted/modular_technology/ttfunk'
|
9
|
+
require 'distorted/modular_technology/vips/save'
|
10
|
+
|
11
|
+
require 'distorted/checking_you_out'
|
12
|
+
|
13
|
+
|
14
|
+
module Cooltrainer; end
|
15
|
+
module Cooltrainer::DistorteD; end
|
16
|
+
module Cooltrainer::DistorteD::Molecule; end
|
17
|
+
module Cooltrainer::DistorteD::Molecule::Text
|
18
|
+
|
19
|
+
#TODO: Generate separate images per-size to stop text being blurry from resizing.
|
20
|
+
|
21
|
+
include Cooltrainer::DistorteD::Technology::TTFunk
|
22
|
+
include Cooltrainer::DistorteD::Technology::Pango
|
23
|
+
include Cooltrainer::DistorteD::Technology::Vips::Save
|
24
|
+
|
25
|
+
# Track supported fonts by codepage.
|
26
|
+
# Avoid renaming these from the original archives / websites.
|
27
|
+
# Try not to go nuts here bloating the size of our Gem for a
|
28
|
+
# very niche feature, but I want to ensure good coverage too.
|
29
|
+
#
|
30
|
+
# Treat codepage 8859 documents as codepage 1252 to avoid breaking smart-
|
31
|
+
# quotes and other printable chars in 1252 that are control chars in 8859.
|
32
|
+
# https://encoding.spec.whatwg.org/#names-and-labels
|
33
|
+
#
|
34
|
+
# Numeric key for UTF-8 is codepage 65001 like Win32:
|
35
|
+
# https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
|
36
|
+
FONT_FILENAME = {
|
37
|
+
:anonpro => 'Anonymous Pro.ttf'.freeze,
|
38
|
+
:anonpro_b => 'Anonymous Pro B.ttf'.freeze,
|
39
|
+
:anonpro_bi => 'Anonymous Pro BI.ttf'.freeze,
|
40
|
+
:anonpro_i => 'Anonymous Pro I.ttf'.freeze,
|
41
|
+
:lessperfectdosvga => 'LessPerfectDOSVGA.ttf'.freeze,
|
42
|
+
:moreperfectdisvga => 'MorePerfectDOSVGA.ttf'.freeze,
|
43
|
+
:perfectdosvgawin => 'Perfect DOS VGA 437 Win.ttf'.freeze,
|
44
|
+
:mona => 'mona.ttf'.freeze,
|
45
|
+
:perfectdosvga => 'Perfect DOS VGA 437.ttf'.freeze,
|
46
|
+
:profont => 'ProFontWindows.ttf'.freeze,
|
47
|
+
:profont_b => 'ProFontWindows-Bold.ttf'.freeze,
|
48
|
+
}
|
49
|
+
# Certain fonts are more suitable for certain codepages,
|
50
|
+
# so track each codepage's available fonts…
|
51
|
+
CODEPAGE_FONT = {
|
52
|
+
65001 => [
|
53
|
+
:anonpro,
|
54
|
+
:anonpro_b,
|
55
|
+
:anonpro_bi,
|
56
|
+
:anonpro_i,
|
57
|
+
],
|
58
|
+
1252 => [
|
59
|
+
:lessperfectdosvga,
|
60
|
+
:moreperfectdosvga,
|
61
|
+
:perfectdosvgawin,
|
62
|
+
],
|
63
|
+
932 => [
|
64
|
+
:mona,
|
65
|
+
],
|
66
|
+
850 => [
|
67
|
+
:profont,
|
68
|
+
:profont_b,
|
69
|
+
],
|
70
|
+
437 => [
|
71
|
+
:perfectdosvga,
|
72
|
+
],
|
73
|
+
}
|
74
|
+
# …as well as the inverse, the numeric codepage for each font:
|
75
|
+
FONT_CODEPAGE = self::CODEPAGE_FONT.reduce(Hash.new([])) { |memo, (key, values)|
|
76
|
+
values.each { |value| memo[value] = key }
|
77
|
+
memo
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
LOWER_WORLD = CHECKING::YOU::IN(/^text\/(plain|x-nfo)/).to_hash.transform_values { |v| Hash[
|
82
|
+
:encoding => Cooltrainer::Compound.new(:encoding, blurb: 'Character encoding used in this document.', default: 'UTF-8'.freeze),
|
83
|
+
]}
|
84
|
+
OUTER_LIMITS = CHECKING::YOU::IN(/^text\/(plain|x-nfo)/).to_hash.merge(
|
85
|
+
Cooltrainer::DistorteD::Technology::Vips::Save::OUTER_LIMITS.dup.transform_values{ |v| Hash[
|
86
|
+
:spacing => Cooltrainer::Compound.new(:spacing, blurb: 'Document-wide character spacing style.', valid: Set[:monospace, :proportional]),
|
87
|
+
:dpi => Cooltrainer::Compound.new(:dpi, blurb: 'Dots per inch for text rendering.', valid: Integer, default: 144),
|
88
|
+
:font => Cooltrainer::Compound.new(:font, blurb: 'Font to use for text rendering.', valid: self::FONT_FILENAME.keys.to_set),
|
89
|
+
]}
|
90
|
+
)
|
91
|
+
|
92
|
+
self::LOWER_WORLD.keys.each { |t|
|
93
|
+
define_method(t.distorted_file_method) { |dest_root, change|
|
94
|
+
copy_file(change.paths(dest_root).first)
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
# Return a Pango Markup escaped version of the document.
|
100
|
+
def to_pango
|
101
|
+
# https://developer.gnome.org/glib/stable/glib-Simple-XML-Subset-Parser.html#g-markup-escape-text
|
102
|
+
escaped = text_file_utf8_content.map{ |c|
|
103
|
+
g_markup_escape_char(c)
|
104
|
+
}
|
105
|
+
if font_spacing == :monospace
|
106
|
+
"<tt>" << escaped << "</tt>"
|
107
|
+
else
|
108
|
+
escaped
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
protected
|
113
|
+
|
114
|
+
def text_file_content
|
115
|
+
# VIPS makes us provide the text content as a single variable,
|
116
|
+
# so we may as well just one-shot File.read() it into memory.
|
117
|
+
# https://kunststube.net/encoding/
|
118
|
+
@text_file_content ||= File.read(path)
|
119
|
+
end
|
120
|
+
|
121
|
+
def text_file_utf8_content
|
122
|
+
CharlockHolmes::Converter.convert(text_file_content, text_file_encoding.to_s, 'UTF-8'.freeze)
|
123
|
+
end
|
124
|
+
|
125
|
+
def text_file_encoding
|
126
|
+
# It's not easy or even possible in some cases to tell the "true" codepage
|
127
|
+
# we should use for any given text document, but using character detection
|
128
|
+
# is worth a shot if the user gave us nothing.
|
129
|
+
#
|
130
|
+
# TODO: Figure out if/how we can get IBM437 files to not be detected as ISO-8859-1
|
131
|
+
@text_file_encoding ||= Encoding::find(
|
132
|
+
abstract(:encoding).to_s ||
|
133
|
+
CharlockHolmes::EncodingDetector.detect(text_file_content)[:encoding] ||
|
134
|
+
'UTF-8'.freeze
|
135
|
+
)
|
136
|
+
end
|
137
|
+
|
138
|
+
def vips_font
|
139
|
+
# Set the shorthand Symbol key for our chosen font.
|
140
|
+
return abstract(:font)&.to_sym || CODEPAGE_FONT[text_file_encoding.code_page].first
|
141
|
+
end
|
142
|
+
|
143
|
+
def to_vips_image
|
144
|
+
# Load font metadata directly from the file so we don't have to
|
145
|
+
# duplicate it here to feed to Vips/Pango.
|
146
|
+
#
|
147
|
+
# irb(main)> font_meta.name.font_name
|
148
|
+
# => ["Perfect DOS VGA 437", "\x00P\x00e\x00r\x00f\x00e\x00c\x00t\x00 \x00D\x00O\x00S\x00 \x00V\x00G\x00A\x00 \x004\x003\x007"]
|
149
|
+
# irb(main)> font_meta.name.font_family
|
150
|
+
# => ["Perfect DOS VGA 437", "\x00P\x00e\x00r\x00f\x00e\x00c\x00t\x00 \x00D\x00O\x00S\x00 \x00V\x00G\x00A\x00 \x004\x003\x007"]
|
151
|
+
# irb(main)> font_meta.name.font_subfamily
|
152
|
+
# => ["Regular", "\x00R\x00e\x00g\x00u\x00l\x00a\x00r"]
|
153
|
+
# irb(main)> font_meta.name.postscript_name
|
154
|
+
# => "PerfectDOSVGA437"
|
155
|
+
# irb(main)> font_meta.line_gap
|
156
|
+
# => 0
|
157
|
+
|
158
|
+
# https://libvips.github.io/libvips/API/current/libvips-create.html#vips-text
|
159
|
+
Vips::Image.text(
|
160
|
+
# This string must be well-escaped Pango Markup:
|
161
|
+
# https://developer.gnome.org/pango/stable/pango-Markup.html
|
162
|
+
# However the official function for escaping text is
|
163
|
+
# not implemented in Ruby GLib, so we have to do it ourselves.
|
164
|
+
to_pango,
|
165
|
+
**{
|
166
|
+
# String absolute path to TTF
|
167
|
+
:fontfile => font_path,
|
168
|
+
# It's not enough to just specify the TTF path;
|
169
|
+
# we must also specify a font family, subfamily, and size.
|
170
|
+
:font => "#{font_name} 16",
|
171
|
+
# Space between lines (in Points).
|
172
|
+
:spacing => to_ttfunk.line_gap,
|
173
|
+
:justify => true, # Requires libvips 8.8
|
174
|
+
:dpi => abstract(:dpi)&.to_i,
|
175
|
+
},
|
176
|
+
)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Return the String absolute path to the TTF file
|
180
|
+
def font_path
|
181
|
+
File.join(
|
182
|
+
File.dirname(__FILE__), # molecule
|
183
|
+
'..'.freeze, # distorted
|
184
|
+
'..'.freeze, # lib
|
185
|
+
'..'.freeze, # DistorteD-Floor
|
186
|
+
'font'.freeze,
|
187
|
+
font_codepage.to_s,
|
188
|
+
font_filename,
|
189
|
+
)
|
190
|
+
end
|
191
|
+
|
192
|
+
# Returns the numeric representation of the codepage
|
193
|
+
# covered by our font.
|
194
|
+
def font_codepage
|
195
|
+
FONT_CODEPAGE.dig(vips_font).to_s
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns the basename (with file extension) of our font.
|
199
|
+
def font_filename
|
200
|
+
FONT_FILENAME.dig(vips_font)
|
201
|
+
end
|
202
|
+
|
203
|
+
end # Text
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'distorted/monkey_business/set'
|
3
|
+
|
4
|
+
require 'distorted/checking_you_out'
|
5
|
+
|
6
|
+
require 'distorted/modular_technology/gstreamer'
|
7
|
+
|
8
|
+
|
9
|
+
module Cooltrainer; end
|
10
|
+
module Cooltrainer::DistorteD; end
|
11
|
+
module Cooltrainer::DistorteD::Molecule; end
|
12
|
+
module Cooltrainer::DistorteD::Molecule::Video
|
13
|
+
|
14
|
+
LOWER_WORLD = CHECKING::YOU::IN('video/mp4').to_hash
|
15
|
+
|
16
|
+
include Cooltrainer::DistorteD::Technology::GStreamer
|
17
|
+
|
18
|
+
end # Video
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'distorted/checking_you_out'
|
4
|
+
|
5
|
+
require 'distorted/triple_counter'
|
6
|
+
GST_MINIMUM_VER = TripleCounter.new(1, 18, 0)
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'gst'
|
10
|
+
GST_AVAILABLE_VER = TripleCounter.new(*(Gst.version))
|
11
|
+
unless GST_AVAILABLE_VER >= GST_MINIMUM_VER
|
12
|
+
raise LoadError.new(
|
13
|
+
"DistorteD needs GStreamer #{GST_MINIMUM_VER}, but the available version is '#{Gst.version_string}'"
|
14
|
+
)
|
15
|
+
end
|
16
|
+
rescue LoadError => le
|
17
|
+
raise unless le.message =~ /libgst/
|
18
|
+
|
19
|
+
# Multiple OS help
|
20
|
+
help = <<~INSTALL
|
21
|
+
|
22
|
+
Please install the GStreamer library for your system, version #{GST_MINIMUM_VER} or later.
|
23
|
+
INSTALL
|
24
|
+
|
25
|
+
# Re-raise with install message
|
26
|
+
raise $!, "#{help}\n#{$!}", $!.backtrace
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
module Cooltrainer; end
|
31
|
+
module Cooltrainer::DistorteD; end
|
32
|
+
module Cooltrainer::DistorteD::Technology; end
|
33
|
+
module Cooltrainer::DistorteD::Technology::GStreamer
|
34
|
+
|
35
|
+
OUTER_LIMITS = CHECKING::YOU::IN(Set[
|
36
|
+
'application/dash+xml',
|
37
|
+
'application/vnd.apple.mpegurl',
|
38
|
+
'video/mp4',
|
39
|
+
])
|
40
|
+
|
41
|
+
|
42
|
+
def write_video_mp4(dest_root, change)
|
43
|
+
copy_file(change.paths(dest_root).first)
|
44
|
+
end
|
45
|
+
|
46
|
+
def write_application_dash_xml(dest, *a, **k)
|
47
|
+
begin
|
48
|
+
segment_dest = File.join(File.dirname(dest), "#{basename}.dash", '/')
|
49
|
+
#segment_dest = segment_dest.sub("#{@base}/", '')
|
50
|
+
FileUtils.mkdir_p(segment_dest)
|
51
|
+
Jekyll.logger.debug(@tag_name, "Re-muxing #{path} to #{segment_dest}")
|
52
|
+
|
53
|
+
# https://gstreamer.freedesktop.org/documentation/tools/gst-launch.html?gi-language=c#pipeline-description
|
54
|
+
# TODO: Convert this from parse_launch() pipeline notation to Element objects
|
55
|
+
# TODO: Get source video duration/resolution/etc and use it to compute a
|
56
|
+
# value for `target-duration`.
|
57
|
+
# TODO: Also support urldecodebin for remote media.
|
58
|
+
pipeline, error = Gst.parse_launch("dashsink name=mux filesrc name=src ! decodebin name=demux ! audioconvert ! avenc_aac ! mux.audio_0 demux. ! videoconvert ! x264enc ! mux.video_0")
|
59
|
+
|
60
|
+
if pipeline.nil?
|
61
|
+
Jekyll.logger.error(@tag_name, "Parse error: #{error.message}")
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
|
65
|
+
filesrc = pipeline.get_by_name('src')
|
66
|
+
filesrc.location = path
|
67
|
+
|
68
|
+
mux = pipeline.get_by_name('mux')
|
69
|
+
mux.mpd_filename = File.basename(dest)
|
70
|
+
mux.target_duration = 3
|
71
|
+
#mux.segment_tpl_path = "#{segment_dest}/#{basename}%05d.mp4"
|
72
|
+
mux.mpd_root_path = segment_dest
|
73
|
+
Jekyll.logger.warn('MPD ROOT PATH', mux.get_property('mpd-root-path'))
|
74
|
+
|
75
|
+
# typedef enum
|
76
|
+
# {
|
77
|
+
# GST_DASH_SINK_MUXER_TS = 0,
|
78
|
+
# GST_DASH_SINK_MUXER_MP4 = 1,
|
79
|
+
# } GstDashSinkMuxerType;
|
80
|
+
mux.muxer = 1
|
81
|
+
|
82
|
+
pipeline.play
|
83
|
+
|
84
|
+
# Play until End Of Stream
|
85
|
+
event_loop(pipeline)
|
86
|
+
|
87
|
+
pipeline.stop
|
88
|
+
|
89
|
+
rescue Gst::ParseError::NoSuchElement
|
90
|
+
raise
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def write_application_vnd_apple_mpegurl(dest, *a, **k)
|
95
|
+
begin
|
96
|
+
orig_dest = dest
|
97
|
+
orig_path = path
|
98
|
+
|
99
|
+
FileUtils.mkdir_p(File.dirname(orig_dest))
|
100
|
+
|
101
|
+
hls_dest = File.join(File.dirname(orig_dest), basename + '.hls')
|
102
|
+
FileUtils.mkdir_p(hls_dest)
|
103
|
+
Jekyll.logger.debug(@tag_name, "Re-muxing #{orig_path} to #{hls_dest}.")
|
104
|
+
|
105
|
+
#FileUtils.rm(orig_dest) if File.exist?(orig_dest)
|
106
|
+
if not File.file?(orig_dest)
|
107
|
+
FileUtils.cp(orig_path, orig_dest)
|
108
|
+
end
|
109
|
+
|
110
|
+
# https://gstreamer.freedesktop.org/documentation/tools/gst-launch.html?gi-language=c#pipeline-description
|
111
|
+
# TODO: Convert this from parse_launch() pipeline notation to Element objects
|
112
|
+
# TODO: Get source video duration/resolution/etc and use it to compute a
|
113
|
+
# value for `target-duration`.
|
114
|
+
# TODO: Also support urldecodebin for remote media.
|
115
|
+
pipeline, error = Gst.parse_launch("filesrc name=src ! decodebin name=demux ! videoconvert ! x264enc ! queue2 ! h264parse ! queue2 ! mux.video hlssink2 name=mux max-files=0 playlist-length=0 target-duration=2 demux. ! audioconvert ! faac ! queue2 ! mux.audio")
|
116
|
+
|
117
|
+
if pipeline.nil?
|
118
|
+
Jekyll.logger.error(@tag_name, "Parse error: #{error.message}")
|
119
|
+
return false
|
120
|
+
end
|
121
|
+
|
122
|
+
filesrc = pipeline.get_by_name('src')
|
123
|
+
filesrc.location = orig_path
|
124
|
+
|
125
|
+
hls_playlist = "#{hls_dest}/#{basename}.m3u8"
|
126
|
+
hls = pipeline.get_by_name('mux')
|
127
|
+
hls.location = "#{hls_dest}/#{basename}%05d.ts"
|
128
|
+
hls.playlist_location = hls_playlist
|
129
|
+
|
130
|
+
# TODO: config option for absolute vs relative segment URIs in the playlist.
|
131
|
+
#hls.playlist_root = @url
|
132
|
+
|
133
|
+
# TODO: dashsink support once there is a stable GStreamer release including it:
|
134
|
+
# https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/merge_requests/704
|
135
|
+
|
136
|
+
pipeline.play
|
137
|
+
|
138
|
+
# Play until End Of Stream
|
139
|
+
event_loop(pipeline)
|
140
|
+
|
141
|
+
pipeline.stop
|
142
|
+
|
143
|
+
# HACK HACK HACK: Replace X-ALLOW-CACHE line in playlist with YES.
|
144
|
+
# This property does not seem to be exposed to the outside of hlssink:
|
145
|
+
# https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/ext/hls/gsthlssink.c
|
146
|
+
text = File.read(hls_playlist)
|
147
|
+
File.write(hls_playlist, text.gsub(/^#EXT-X-ALLOW-CACHE:NO$/, '#EXT-X-ALLOW-CACHE:YES'))
|
148
|
+
rescue Gst::ParseError::NoSuchElement
|
149
|
+
raise
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def event_loop(pipeline)
|
154
|
+
running = true
|
155
|
+
bus = pipeline.bus
|
156
|
+
|
157
|
+
while running
|
158
|
+
message = bus.poll(Gst::MessageType::ANY, -1)
|
159
|
+
|
160
|
+
case message.type
|
161
|
+
when Gst::MessageType::EOS
|
162
|
+
running = false
|
163
|
+
when Gst::MessageType::WARNING
|
164
|
+
warning, _debug = message.parse_warning
|
165
|
+
Jekyll.logger.warning(@tag_name, warning)
|
166
|
+
when Gst::MessageType::ERROR
|
167
|
+
error, _debug = message.parse_error
|
168
|
+
Jekyll.logger.error(@tag_name, error)
|
169
|
+
running = false
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|