Skip to content

Instantly share code, notes, and snippets.

@mldoscar
Created April 12, 2022 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mldoscar/a1677d3f5395510fa3292791728e4b80 to your computer and use it in GitHub Desktop.
Save mldoscar/a1677d3f5395510fa3292791728e4b80 to your computer and use it in GitHub Desktop.
Convierte videos con ffmpeg
# encoding: utf-8
require "fileutils"
if `which ffmpeg`.strip.empty?
raise Exception.new("ffmpeg no esta instalado en el equipo")
end
puts "Arrastre la carpeta del directorio raiz aqui abajo:"
root_directory = gets.chomp.gsub("'", "").gsub("\"", "").strip
raise ArgumentError.new("No es una carpeta") unless Dir.exists?(root_directory)
puts "Arrastre la carpeta donde quiere que se guarden los archivos convertidos aqui abajo:"
new_dir = gets.chomp.gsub("'", "").gsub("\"", "").strip
raise ArgumentError.new("No es una carpeta") unless Dir.exists?(new_dir)
new_dir = File.join(new_dir, "CONVERTIDOS")
files = []
files += Dir[File.join(root_directory, "*.mp4")]
files += Dir[File.join(root_directory, "*", "**.mp4")]
files += Dir[File.join(root_directory, "*", "*", "**.mp4")]
files += Dir[File.join(root_directory, "*", "*", "*", "**.mp4")]
files += Dir[File.join(root_directory, "*", "*", "*", "*", "**.mp4")]
files = files.select { |x| !x.start_with?(new_dir) }.uniq
if files.empty?
raise Exception.new("No hay videos mp4 para convertir")
end
new_entries = []
root_dir_wslash = (root_directory + File::SEPARATOR).gsub(File::SEPARATOR * 2, File::SEPARATOR)
files.each do |file|
abs_path = file.gsub(root_dir_wslash, "")
new_fold = File.join(new_dir, File.dirname(abs_path))
new_entries.push({
original_file: file,
destination_folder: new_fold,
base_name: File.basename(file),
new_file: File.join(new_fold, File.basename(file))
})
end
new_entries.sort_by! { |x| x[:destination_folder] }
length = new_entries.length
i = 0
new_entries.group_by { |x| x[:destination_folder] }.each do |folder, entries|
puts "CARPETA: #{folder}"
FileUtils.mkpath folder unless Dir.exists?(folder)
entries.each do |entry|
percent = (((i+1).to_f / length).round(2) * 100).to_s + "%"
print "Convirtiendo archivo [#{i+1} de #{length}] ... "
File.delete(entry[:new_file]) if File.exists?(entry[:new_file])
`ffmpeg -hide_banner -loglevel error -i "#{entry[:original_file]}" "#{entry[:new_file]}"`
puts "#{percent}"
i += 1
end
end
puts "CONVERSION FINALIZADA!"
puts "PUEDES VISITAR LA CARPETA DE VIDEOS CONVERTIDOS ACA: #{new_dir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment