Ruby 1.9.3环境下,如何在Ruby on Rails中将PPTX/PPT转为MP4?
Hey there! Let's figure out how to convert PPTX/PPT files to MP4 in your Ruby on Rails app using Ruby 1.9.3. Since direct Ruby gems for this specific conversion are pretty scarce, here are the most practical approaches you can use:
This is the go-to method because it leverages robust open-source tools to handle the conversion in two steps: first turning the presentation into image slides, then stitching those images into an MP4 video.
Step 1: Convert PPT/PPTX to Image Slides with LibreOffice
You don't need a special gem for this—Ruby 1.9.3's built-in open3 library lets you call system commands directly. Here's a quick example:
require 'open3' require 'fileutils' def convert_ppt_to_images(ppt_file_path, output_folder) # Create output folder if it doesn't exist FileUtils.mkdir_p(output_folder) unless Dir.exist?(output_folder) # Call LibreOffice in headless mode to export slides as PNGs libreoffice_cmd = "libreoffice --headless --convert-to png --outdir #{output_folder} #{ppt_file_path}" stdout, stderr, status = Open3.capture3(libreoffice_cmd) if status.success? # Return sorted list of image paths to ensure slide order is correct Dir.glob("#{output_folder}/*.png").sort else raise "Failed to convert PPT to images: #{stderr.strip}" end end
Note: You'll need to install LibreOffice on your server first—most Linux distros have it in their package managers, and it's available for macOS/Windows too.
Step 2: Stitch Images to MP4 with FFmpeg
For this, you can use the streamio-ffmpeg gem, but you'll need to lock it to an older version that supports Ruby 1.9.3. Add this to your Gemfile:
gem 'streamio-ffmpeg', '~> 1.0.0'
Run bundle install, then use this code to transcode the images:
require 'streamio-ffmpeg' def convert_images_to_mp4(image_paths, output_mp4_path, slide_duration=1) # FFmpeg uses a pattern to match ordered images—adjust if your filenames differ image_pattern = File.join(File.dirname(image_paths.first), "*.png") movie = FFMPEG::Movie.new(image_pattern) # Transcode with frame rate matching your desired slide duration (1 frame = 1 second here) movie.transcode(output_mp4_path, "-r #{1/slide_duration} -pix_fmt yuv420p") do |progress| puts "Conversion progress: #{progress.round(2)}%" end end
Note: FFmpeg must be installed on your server. This command ensures the output MP4 is compatible with most players using the yuv420p pixel format.
If you're dealing with big presentations, converting them synchronously might slow down your app. Use an older version of Sidekiq (v3.x supports Ruby 1.9.3) to offload the conversion to a background worker. Just wrap the two methods above in a Sidekiq worker class.
- Ruby 1.9.3 is end-of-life, so you'll need to stick to older gem versions that explicitly support it. Always check the gem's README or rubygems.org page for compatibility.
- Make sure your server has enough resources (CPU/RAM) for conversions—LibreOffice and FFmpeg can be resource-heavy with large files.
内容的提问来源于stack exchange,提问作者Sanket

