ffmpeg - 在流程结束之前重命名载波扩展

标签 ffmpeg carrierwave fog

我有一个通过carrierwave 上传的音频文件。我想在处理它之前重命名 current_file。

当我处理一个版本时,通常我通过重写文件扩展名

def full_filename(for_file=file)
  super.chomp(File.extname(super)) + '.mp3'
end 

但这将在版本创建过程之后执行。

如何在保存之前制作版本并重命名它。

更具体一点:

我正在使用 ffmpeg 将 WAV 文件转换为 MP3。

FFMPEG 需要一个 inputfile (-i inputfile.wav) 和 outputfilename,它需要 mp3 文件扩展名来处理 mp3。 (在我的情况下输出.mp3)

如何在保存之前重命名扩展名?
ffmpeg -i inputfile.wav -acodec libmp3lame -f mp3 watermarked.mp3
         HOW CAN I RENAME THE EXTENSTION BEFORE IT GET SAVED? ^^^

上面的片段(-f 强制编解码器和格式)不是它的工作和
def full_filename(for_file=file)
  super.chomp(File.extname(super)) + '.mp3'
end 

发生得太晚(处理后完成)

如何重命名临时 Carrierfi​​le 名称?

最佳答案

您可以通过使用临时文件(带有 mp3 扩展名)来解决此问题,然后将其移动到可以由 full_filename 处理的位置。正如预期的那样:

version :mp3 do
  process :convert_to_mp3

  def convert_to_mp3
    temp_path = ... # generate good temp path, ending in '.mp3'

    `ffmpeg -i #{ current_path.shellescape } -acodec libmp3lame -f mp3 #{ temp_path.shellescape }`

    File.unlink(current_path)
    FileUtils.mv(temp_path, current_path)
  end

  def full_filename(for_file)
    super.chomp(File.extname(super)) + '.mp3'
  end
end

一些用于生成 temp_path 的选项,供您测试和决定:
  • current_path.chomp(File.extname(current_path)) + '.mp3'
  • Tempfile.new([File.basename(current_path), '.mp3']).path
  • Rails.root.join('tmp', 'mp3', Dir::Tmpname.make_tmpname([original_filename,'.mp3'], nil))
  • 关于ffmpeg - 在流程结束之前重命名载波扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18569083/

    相关文章:

    video - FFMpeg - 添加背景音乐

    facebook - 在 Ubuntu 16.04 上使用 ffmpeg 从 Icecast 流式传输到 Facebook Live

    ruby-on-rails-3.1 - Carrierwave Gem - Heroku - Fog Gem 配置 - 给出名称错误

    ruby - CarrierWave 无法将 nil 转换为 String

    ruby-on-rails - Rails + Carrierwave + Ckeditor + Fog + S3 - 文件没有指向亚马逊

    c++ - 使用 libav/ffmpeg 将 RGB8 转换为 NV12

    c++ - YUV420p 到 RGB 的转换改变了 U 和 V 值 - iOS Unity3D

    ruby-on-rails - RMagick提示OSX中找不到libMagickCore.5.dylib

    ruby-on-rails - Carrierwave 通过雾检查远程文件是否存在

    ruby-on-rails - 回形针,fog.io和Rackspace