converted 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6879d746866937e53d3fc99e0a233294bc20c40cc9c3fa169873b5c9ddb3a3bd
4
- data.tar.gz: 2611678c67183686f58c97b0cd143010f34be9e3da86abc64c006bc95dc537c1
3
+ metadata.gz: aa2fa545239520ab86cc59ff22a03d14ec7386dc13f40409776cd8b845316d67
4
+ data.tar.gz: 6cd4141da51a430c83daeb3730ef8ebaaf6568ecdcc10cf0262dd8ab4ebfcc69
5
5
  SHA512:
6
- metadata.gz: f3bd95be8500b92a5b8fa9c09f8415ddcf32568527582ca8dadac9d3502f142c8310c12ed6b54221a10e0769b838d881547d54dd22f27491c14863a5e666216b
7
- data.tar.gz: 3e84db922ba611f30c91602245d8a12e0d3379fd57de1aceb16197e97e1fdb66b85f7a965dd4291688ad686fe1b89d41b842101a1794c3f99f0b58fc0ff11153
6
+ metadata.gz: a0cfc0d124cb33c5a6e36793c4dfab1a6e227e212ddc9125bc2b9cb2b5ee1db14da2d2a8f459f1fdc35e7b01e07e546a515c9ae47e916b72a9b5f9371ac43767
7
+ data.tar.gz: e664f4d3ffe602f9c82f0f7d8051b1d593c37a74aa246da9e1b0065e63baff952fa2cc0f584e39252adbfd5091cfc320d50a0bbdf2679086fc3075d12efe8638
data/.standard.yml ADDED
@@ -0,0 +1,2 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at admin@ianrandmckenzie.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in converted.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ converted (0.1.0)
5
+ fileutils
6
+ minitest
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ fileutils (1.7.3)
13
+ json (2.8.2)
14
+ language_server-protocol (3.17.0.3)
15
+ lint_roller (1.1.0)
16
+ minitest (5.25.2)
17
+ parallel (1.26.3)
18
+ parser (3.3.6.0)
19
+ ast (~> 2.4.1)
20
+ racc
21
+ racc (1.8.1)
22
+ rainbow (3.1.1)
23
+ rake (13.2.1)
24
+ regexp_parser (2.9.2)
25
+ rexml (3.3.9)
26
+ rubocop (1.64.1)
27
+ json (~> 2.3)
28
+ language_server-protocol (>= 3.17.0)
29
+ parallel (~> 1.10)
30
+ parser (>= 3.3.0.2)
31
+ rainbow (>= 2.2.2, < 4.0)
32
+ regexp_parser (>= 1.8, < 3.0)
33
+ rexml (>= 3.2.5, < 4.0)
34
+ rubocop-ast (>= 1.31.1, < 2.0)
35
+ ruby-progressbar (~> 1.7)
36
+ unicode-display_width (>= 2.4.0, < 3.0)
37
+ rubocop-ast (1.36.1)
38
+ parser (>= 3.3.1.0)
39
+ rubocop-performance (1.21.1)
40
+ rubocop (>= 1.48.1, < 2.0)
41
+ rubocop-ast (>= 1.31.1, < 2.0)
42
+ ruby-progressbar (1.13.0)
43
+ standard (1.37.0)
44
+ language_server-protocol (~> 3.17.0.2)
45
+ lint_roller (~> 1.0)
46
+ rubocop (~> 1.64.0)
47
+ standard-custom (~> 1.0.0)
48
+ standard-performance (~> 1.4)
49
+ standard-custom (1.0.2)
50
+ lint_roller (~> 1.0)
51
+ rubocop (~> 1.50)
52
+ standard-performance (1.4.0)
53
+ lint_roller (~> 1.1)
54
+ rubocop-performance (~> 1.21.0)
55
+ unicode-display_width (2.6.0)
56
+
57
+ PLATFORMS
58
+ x86_64-darwin-20
59
+
60
+ DEPENDENCIES
61
+ converted!
62
+ minitest (~> 5.0)
63
+ rake (~> 13.0)
64
+ standard (~> 1.3)
65
+
66
+ BUNDLED WITH
67
+ 2.2.33
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Curious Markings, Co.
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,92 @@
1
+ # Converted
2
+
3
+ Converted is a shoot-from-the-hip media asset conversion system. It aims to convert video, audio, images, and text into alternate formats without any additional configuration or modification needed. This is a one-size-fits-all approach and may not be appropriate if you desire specific outcomes for your converted file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'converted'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```sh
16
+ bundle install
17
+ ```
18
+ Or install it yourself as:
19
+ ```sh
20
+ gem install converted
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### PNG Example
26
+
27
+ ```rb
28
+ converter = Converted::PNG.new('/root/someuser/converted/test/test_assets/image/test_image.png')
29
+
30
+ converter.convert_to_jpg('/root/someuser/somefolder/image.jpg')
31
+ converter.convert_to_gif('/root/someuser/somefolder/image.gif')
32
+ converter.convert_to_webp('/root/someuser/somefolder/image.webp')
33
+ converter.convert_to_avif('/root/someuser/somefolder/image.avif')
34
+ converter.convert_to_bmp('/root/someuser/somefolder/image.bmp', with_transparency: false)
35
+ # preverse transparency, but reduce color range from 32-bit to 24-bit
36
+ converter.convert_to_bmp('/root/someuser/somefolder/image.bmp', with_transparency: true)
37
+ ```
38
+
39
+ Note: You can use `irb` to test from terminal:
40
+ ```sh
41
+ # For example, png conversion
42
+ irb -Ilib -r converted/png
43
+ ```
44
+
45
+ ### Other options
46
+ 1. gif to:
47
+ * jpg
48
+ * png
49
+ * webp
50
+ * avif
51
+ * bmp
52
+ * ico
53
+ * webm
54
+ * avi
55
+ * mp4
56
+ 2. jpg to:
57
+ * gif
58
+ * png
59
+ * webp
60
+ * avif
61
+ * bmp
62
+ * ico
63
+ 3. mp4 to:
64
+ * mp3
65
+ * gif
66
+ * webm
67
+ * avi
68
+ 4. png to:
69
+ * jpg
70
+ * gif
71
+ * webp
72
+ * avif
73
+ * bmp
74
+ * ico
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/curiousmarkingsco/converted. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/curiousmarkingsco/converted/blob/main/CODE_OF_CONDUCT.md).
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the Converted project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/curiousmarkingsco/converted/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
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 "converted"
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
Binary file
@@ -0,0 +1,90 @@
1
+ # lib/converted/gif.rb
2
+ require 'fileutils'
3
+
4
+ module Converted
5
+ class GIF
6
+ TMP_DIR = File.join(Dir.pwd, 'tmp') # Define the tmp directory path
7
+
8
+ def initialize(input_file)
9
+ @input_file = input_file
10
+ unless File.exist?(@input_file)
11
+ raise "Error: Input file #{@input_file} does not exist."
12
+ end
13
+ end
14
+
15
+ def convert_to_jpg(output_file = nil)
16
+ output_file ||= tmp_file(change_extension(@input_file, 'jpg'))
17
+ run_ffmpeg_command("-vf 'format=yuvj422p,pad=iw:ih:0:0:color=white'", output_file, "JPG")
18
+ end
19
+
20
+ def convert_to_png(output_file = nil)
21
+ output_file ||= tmp_file(change_extension(@input_file, 'png'))
22
+ run_ffmpeg_command("", output_file, "PNG")
23
+ end
24
+
25
+ def convert_to_webp(output_file = nil)
26
+ output_file ||= tmp_file(change_extension(@input_file, 'webp'))
27
+ run_ffmpeg_command("-c:v libwebp -lossless 1", output_file, "WEBP")
28
+ end
29
+
30
+ def convert_to_avif(output_file = nil)
31
+ output_file ||= tmp_file(change_extension(@input_file, 'avif'))
32
+ run_ffmpeg_command("-c:v libaom-av1 -crf 30", output_file, "AVIF")
33
+ end
34
+
35
+ def convert_to_bmp(output_file = nil, with_transparency: false)
36
+ output_file ||= tmp_file(change_extension(@input_file, 'bmp'))
37
+ options = with_transparency ? "" : "-vf 'format=bgr24,pad=iw:ih:0:0:color=white'"
38
+ run_ffmpeg_command(options, output_file, "BMP")
39
+ end
40
+
41
+ def convert_to_ico(output_file = nil)
42
+ output_file ||= tmp_file(change_extension(@input_file, 'ico'))
43
+ run_ffmpeg_command("", output_file, "ICO")
44
+ end
45
+
46
+ def convert_to_webm(output_file = nil)
47
+ output_file ||= tmp_file(change_extension(@input_file, 'webm'))
48
+ run_ffmpeg_command("-c:v libvpx-vp9 -b:v 1M -an", output_file, "WEBM")
49
+ end
50
+
51
+ def convert_to_avi(output_file = nil)
52
+ output_file ||= tmp_file(change_extension(@input_file, 'avi'))
53
+ run_ffmpeg_command("-c:v mpeg4 -vtag xvid -qscale:v 3", output_file, "AVI")
54
+ end
55
+
56
+ def convert_to_mp4(output_file = nil)
57
+ output_file ||= tmp_file(change_extension(@input_file, 'mp4'))
58
+ run_ffmpeg_command("-c:v libx264 -crf 23 -preset fast", output_file, "MP4")
59
+ end
60
+
61
+ private
62
+
63
+ def run_ffmpeg_command(options, output_file, format)
64
+ unless ffmpeg_installed?
65
+ raise "Error: ffmpeg is not installed or not in the system PATH."
66
+ end
67
+
68
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
69
+ command = "ffmpeg -i \"#{@input_file}\" #{options} \"#{output_file}\" -y"
70
+ puts "Converting to #{format}: #{output_file}..."
71
+ if system(command)
72
+ puts "#{format} conversion complete: #{output_file}"
73
+ else
74
+ puts "Error: #{format} conversion failed."
75
+ end
76
+ end
77
+
78
+ def ffmpeg_installed?
79
+ system('ffmpeg -version > /dev/null 2>&1')
80
+ end
81
+
82
+ def change_extension(file, new_extension)
83
+ File.basename(file, File.extname(file)) + ".#{new_extension}"
84
+ end
85
+
86
+ def tmp_file(filename)
87
+ File.join(TMP_DIR, filename)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,76 @@
1
+ # lib/converted/jpg.rb
2
+ require 'fileutils'
3
+
4
+ module Converted
5
+ class JPG
6
+ TMP_DIR = File.join(Dir.pwd, 'tmp') # Define the tmp directory path
7
+
8
+ def initialize(input_file)
9
+ @input_file = input_file
10
+ unless File.exist?(@input_file)
11
+ raise "Error: Input file #{@input_file} does not exist."
12
+ end
13
+ end
14
+
15
+ def convert_to_gif(output_file = nil)
16
+ output_file ||= tmp_file(change_extension(@input_file, 'gif'))
17
+ run_ffmpeg_command("-vf 'palettegen,paletteuse'", output_file, "GIF")
18
+ end
19
+
20
+ def convert_to_png(output_file = nil)
21
+ output_file ||= tmp_file(change_extension(@input_file, 'png'))
22
+ run_ffmpeg_command("", output_file, "PNG")
23
+ end
24
+
25
+ # For WEBP and AVIF, we could tweak compression settings (-crf, etc.)
26
+ # if higher quality or smaller file size is desired.
27
+ def convert_to_webp(output_file = nil)
28
+ output_file ||= tmp_file(change_extension(@input_file, 'webp'))
29
+ run_ffmpeg_command("-c:v libwebp -lossless 0", output_file, "WEBP")
30
+ end
31
+
32
+ def convert_to_avif(output_file = nil)
33
+ output_file ||= tmp_file(change_extension(@input_file, 'avif'))
34
+ run_ffmpeg_command("-c:v libaom-av1 -crf 30", output_file, "AVIF")
35
+ end
36
+
37
+ def convert_to_bmp(output_file = nil)
38
+ output_file ||= tmp_file(change_extension(@input_file, 'bmp'))
39
+ run_ffmpeg_command("-vf 'format=bgr24'", output_file, "BMP")
40
+ end
41
+
42
+ def convert_to_ico(output_file = nil)
43
+ output_file ||= tmp_file(change_extension(@input_file, 'ico'))
44
+ run_ffmpeg_command("-vf \"scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:white\"", output_file, "ICO")
45
+ end
46
+
47
+ private
48
+
49
+ def run_ffmpeg_command(options, output_file, format)
50
+ unless ffmpeg_installed?
51
+ raise "Error: ffmpeg is not installed or not in the system PATH."
52
+ end
53
+
54
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
55
+ command = "ffmpeg -i \"#{@input_file}\" #{options} \"#{output_file}\" -y"
56
+ puts "Converting to #{format}: #{output_file}..."
57
+ if system(command)
58
+ puts "#{format} conversion complete: #{output_file}"
59
+ else
60
+ puts "Error: #{format} conversion failed."
61
+ end
62
+ end
63
+
64
+ def ffmpeg_installed?
65
+ system('ffmpeg -version > /dev/null 2>&1')
66
+ end
67
+
68
+ def change_extension(file, new_extension)
69
+ File.basename(file, File.extname(file)) + ".#{new_extension}"
70
+ end
71
+
72
+ def tmp_file(filename)
73
+ File.join(TMP_DIR, filename)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,81 @@
1
+ # lib/converted/mp4.rb
2
+ require 'fileutils'
3
+
4
+ module Converted
5
+ class MP4
6
+ MAX_GIF_SIZE_MB = 10
7
+
8
+ TMP_DIR = File.join(Dir.pwd, 'tmp') # Define the tmp directory path
9
+
10
+ def initialize(input_file)
11
+ @input_file = input_file
12
+ unless File.exist?(@input_file)
13
+ raise "Error: Input file #{@input_file} does not exist."
14
+ end
15
+ end
16
+
17
+ def convert_to_mp3(output_file = nil)
18
+ output_file ||= tmp_file(change_extension(@input_file, 'mp3'))
19
+ run_ffmpeg_command("-q:a 0 -map a", output_file, "MP3")
20
+ end
21
+
22
+ def convert_to_gif(output_file = nil)
23
+ output_file ||= tmp_file(change_extension(@input_file, 'gif'))
24
+
25
+ temp_gif = tmp_file("temp_#{File.basename(output_file)}")
26
+ run_ffmpeg_command("-vf 'fps=10,scale=320:-1:flags=lanczos' -t 10", temp_gif, "GIF")
27
+
28
+ gif_size_mb = File.size(temp_gif).to_f / (1024 * 1024)
29
+ if gif_size_mb > MAX_GIF_SIZE_MB
30
+ puts "GIF size (#{gif_size_mb.round(2)} MB) exceeds the maximum allowed size of #{MAX_GIF_SIZE_MB} MB."
31
+ File.delete(temp_gif) if File.exist?(temp_gif)
32
+ else
33
+ FileUtils.mv(temp_gif, output_file)
34
+ puts "GIF conversion successful: #{output_file}"
35
+ end
36
+ end
37
+
38
+ def convert_to_webm(output_file = nil)
39
+ output_file ||= tmp_file(change_extension(@input_file, 'webm'))
40
+ run_ffmpeg_command("-c:v libvpx-vp9 -b:v 1M -c:a libopus", output_file, "WEBM")
41
+ end
42
+
43
+ def convert_to_avi(output_file = nil)
44
+ output_file ||= tmp_file(change_extension(@input_file, 'avi'))
45
+ run_ffmpeg_command("-c:v mpeg4 -vtag xvid -qscale:v 3", output_file, "AVI")
46
+ end
47
+
48
+ private
49
+
50
+ # Run the ffmpeg command for a given format
51
+ def run_ffmpeg_command(options, output_file, format)
52
+ unless ffmpeg_installed?
53
+ raise "Error: ffmpeg is not installed or not in the system PATH."
54
+ end
55
+
56
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
57
+ command = "ffmpeg -i \"#{@input_file}\" #{options} \"#{output_file}\" -y"
58
+ puts "Converting to #{format}: #{output_file}..."
59
+ if system(command)
60
+ puts "#{format} conversion complete: #{output_file}"
61
+ else
62
+ puts "Error: #{format} conversion failed."
63
+ end
64
+ end
65
+
66
+ # Check if ffmpeg is installed
67
+ def ffmpeg_installed?
68
+ system('ffmpeg -version > /dev/null 2>&1')
69
+ end
70
+
71
+ # Change the file extension
72
+ def change_extension(file, new_extension)
73
+ File.basename(file, File.extname(file)) + ".#{new_extension}"
74
+ end
75
+
76
+ # Prepend the TMP_DIR path to a filename
77
+ def tmp_file(filename)
78
+ File.join(TMP_DIR, filename)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,120 @@
1
+ # lib/converted/png.rb
2
+ require 'fileutils'
3
+
4
+ module Converted
5
+ class PNG
6
+ TMP_DIR = File.join(Dir.pwd, 'tmp') # Define the tmp directory path
7
+
8
+ def initialize(input_file)
9
+ @input_file = input_file
10
+ unless File.exist?(@input_file)
11
+ raise "Error: Input file #{@input_file} does not exist."
12
+ end
13
+ end
14
+
15
+ def convert_to_jpg(output_file = nil)
16
+ output_file ||= tmp_file(change_extension(@input_file, 'jpg'))
17
+ run_ffmpeg_command("-vf 'format=yuvj422p,scale=trunc(iw/2)*2:trunc(ih/2)*2'", output_file, "JPG", white_background: true)
18
+ end
19
+
20
+ def convert_to_gif(output_file = nil)
21
+ output_file ||= tmp_file(change_extension(@input_file, 'gif'))
22
+ palette_file = tmp_file('palette.png')
23
+
24
+ unless ffmpeg_installed?
25
+ raise "Error: ffmpeg is not installed or not in the system PATH."
26
+ end
27
+
28
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
29
+
30
+ # Generate a palette for better GIF quality
31
+ palettegen_command = "ffmpeg -i \"#{@input_file}\" -vf \"palettegen\" \"#{palette_file}\" -y"
32
+ puts "Generating palette for GIF conversion..."
33
+ system(palettegen_command)
34
+
35
+ if File.exist?(palette_file)
36
+ # Use the palette for the actual GIF conversion
37
+ gif_command = "ffmpeg -i \"#{@input_file}\" -i \"#{palette_file}\" -filter_complex \"paletteuse\" \"#{output_file}\" -y"
38
+ puts "Converting to GIF: #{output_file}..."
39
+ if system(gif_command)
40
+ puts "GIF conversion complete: #{output_file}"
41
+ else
42
+ puts "Error: GIF conversion failed."
43
+ end
44
+ File.delete(palette_file) if File.exist?(palette_file) # Clean up temporary palette file
45
+ else
46
+ puts "Error: Failed to generate palette for GIF conversion."
47
+ end
48
+ end
49
+
50
+ def convert_to_webp(output_file = nil)
51
+ output_file ||= tmp_file(change_extension(@input_file, 'webp'))
52
+ run_ffmpeg_command("-c:v libwebp -lossless 1", output_file, "WEBP", white_background: false)
53
+ end
54
+
55
+ def convert_to_avif(output_file = nil)
56
+ output_file ||= tmp_file(change_extension(@input_file, 'avif'))
57
+ run_ffmpeg_command("-c:v libaom-av1 -crf 30", output_file, "AVIF", white_background: false)
58
+ end
59
+
60
+ def convert_to_bmp(output_file = nil, with_transparency: false)
61
+ output_file ||= tmp_file(change_extension(@input_file, 'bmp'))
62
+ options = with_transparency ? "" : "-vf 'format=bgr24'"
63
+ run_ffmpeg_command(options, output_file, "BMP", white_background: !with_transparency)
64
+ end
65
+
66
+ def convert_to_ico(output_file = nil)
67
+ output_file ||= tmp_file(change_extension(@input_file, 'ico'))
68
+
69
+ unless ffmpeg_installed?
70
+ raise "Error: ffmpeg is not installed or not in the system PATH."
71
+ end
72
+
73
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
74
+
75
+ # Resize to a standard ICO size (e.g., 256x256 for high-quality icons)
76
+ resize_command = "ffmpeg -i \"#{@input_file}\" -vf \"scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:white\" \"#{output_file}\" -y"
77
+ puts "Converting to ICO with resizing: #{output_file}..."
78
+ if system(resize_command)
79
+ puts "ICO conversion complete: #{output_file}"
80
+ else
81
+ puts "Error: ICO conversion failed."
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def run_ffmpeg_command(options, output_file, format, white_background: false)
88
+ unless ffmpeg_installed?
89
+ raise "Error: ffmpeg is not installed or not in the system PATH."
90
+ end
91
+
92
+ FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists
93
+ command = if white_background
94
+ # Add `-vf "format=yuvj422p,pad=color=white"` for white background
95
+ "ffmpeg -i \"#{@input_file}\" -vf \"pad=iw:ih:0:0:color=white\" #{options} \"#{output_file}\" -y"
96
+ else
97
+ "ffmpeg -i \"#{@input_file}\" #{options} \"#{output_file}\" -y"
98
+ end
99
+
100
+ puts "Converting to #{format}: #{output_file}..."
101
+ if system(command)
102
+ puts "#{format} conversion complete: #{output_file}"
103
+ else
104
+ puts "Error: #{format} conversion failed."
105
+ end
106
+ end
107
+
108
+ def ffmpeg_installed?
109
+ system('ffmpeg -version > /dev/null 2>&1')
110
+ end
111
+
112
+ def change_extension(file, new_extension)
113
+ File.basename(file, File.extname(file)) + ".#{new_extension}"
114
+ end
115
+
116
+ def tmp_file(filename)
117
+ File.join(TMP_DIR, filename)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Converted
4
+ VERSION = "0.1.1"
5
+ end
data/lib/converted.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "converted/version"
4
+ require_relative 'converted/mp4'
5
+ require_relative 'converted/png'
6
+ require_relative 'converted/gif'
7
+ require_relative 'converted/jpg'
8
+
9
+ module Converted
10
+ class Error < StandardError; end
11
+ end
data/sig/converted.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Converted
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: converted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian R. McKenzie
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-26 00:00:00.000000000 Z
11
+ date: 2025-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fileutils
@@ -38,13 +38,31 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description:
41
+ description:
42
42
  email:
43
43
  - ian@curiousmarkings.com
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
- files: []
47
+ files:
48
+ - ".standard.yml"
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - converted-0.1.0.gem
59
+ - lib/converted.rb
60
+ - lib/converted/gif.rb
61
+ - lib/converted/jpg.rb
62
+ - lib/converted/mp4.rb
63
+ - lib/converted/png.rb
64
+ - lib/converted/version.rb
65
+ - sig/converted.rbs
48
66
  homepage: https://github.com/curiousmarkingsco/converted
49
67
  licenses:
50
68
  - MIT
@@ -53,7 +71,7 @@ metadata:
53
71
  homepage_uri: https://github.com/curiousmarkingsco/converted
54
72
  source_code_uri: https://github.com/curiousmarkingsco/converted
55
73
  changelog_uri: https://github.com/curiousmarkingsco/converted/blob/main/CHANGELOG.md
56
- post_install_message:
74
+ post_install_message:
57
75
  rdoc_options: []
58
76
  require_paths:
59
77
  - lib
@@ -68,8 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
86
  - !ruby/object:Gem::Version
69
87
  version: '0'
70
88
  requirements: []
71
- rubygems_version: 3.1.2
72
- signing_key:
89
+ rubygems_version: 3.5.3
90
+ signing_key:
73
91
  specification_version: 4
74
92
  summary: Convert media like images, video, audio, and text that are in commonly used
75
93
  formats into other commonly used formats.