首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >维护并行的无损和无损的音乐收藏

维护并行的无损和无损的音乐收藏
EN

Unix & Linux用户
提问于 2022-06-11 20:44:50
回答 1查看 115关注 0票数 1

我收藏了大量的音乐。有些文件是无损的,有些文件是有损的。

我想维护一个由原始集合的有损文件和原始集合无损文件的有损转换代码组成的集合的副本。

一些假设:

  • 我知道如何使用ffmpeg将flac转换为opus文件。
  • 我只有需要转换的flac文件,没有wav或alac编解码器。
  • 有损文件可以是opus、vorbis或mp3。

我想:

  • 对新的音乐集合使用最小的存储空间。也就是说,它将在适当情况下链接回原始有损文件。
  • 在我将更多有损和无损的文件添加到原始文件或更新元数据时,请保持集合的最新情况。
  • 无需重新编码未被修改的无损文件。

我想我需要使用一些自定义脚本来完成这个任务,但是如果在我投入大量时间之前,任何人都有建议或提示,我将永远感激。

EN

回答 1

Unix & Linux用户

发布于 2022-06-11 22:40:43

我不喜欢Makefiles (我可能同意 guy),但是,make可以随心所欲地做你想做的事情:

例如,您定义了一条规则,希望为每个源.opus文件提供一个.flac文件:

Makefile,从我头顶

代码语言:javascript
复制
TARGETDIR=/path/to/compressed/library
%.opus: %.flac
    ffmpeg -ffmpegflags -and -stuff -i "$<" -o "$@"
$(TARGETDIR)/%.opus: %.opus
    cp --reflink=always "$<" "$@"

它可以将所有的FLACs转换为树中的OPUSes。只有当.opus文件还没有存在,或者比FLAC上一次更改更早的时候,它才能做到这一点。

我不喜欢它,因为它发生在-tree中,也就是说,您不会得到一个干净的“纯原始”目录。至少在文件系统上使用一个支持反射的cp,这样你的拷贝就很浅了(而且实际上不需要任何空间)。它也不优雅地处理子目录,我认为,您会发现

那么,老实说,make在这里的功能实际上是:

对于每个通配符源文件(%.flac),检查结果(相同的文件.opus)是否已经生成,如果没有(或者构建比源文件更早),则执行构建。

这是有点向后,也没有复杂到足以依赖于制造。所以,shell脚本。我用zsh。虽然我不测试我写的东西,但我试着评论一下:

代码语言:javascript
复制
#!/usr/bin/zsh
# Copyright 2022 Marcus Müller
# SPDX-License-Identifier: BSD-3-Clause
# Find the license text under https://spdx.org/licenses/BSD-3-Clause.html

# set options:
setopt null_glob    # Don't fail if there's no file matching a pattern
setopt no_case_glob # Don't care about case in matching

TARGET_DIR=../compressed_library

make_containing_dir() {
  target_dir="${1:h}"
  if [[ ! -d "${target_dir}" ]] ; then
    logger -p user.debug "Creating directory ${target_dir}"
    mkdir -p "${target_dir}" || logger -p user.err "can't mkdir ${target_dir}"
}

for compressed_source in **/*.{mp3,opus,vorbis,mp4} ; do
  if [[ -d "${compressed_source}" ]]; then
    continue # skip directories that happen to have a matching suffix
  fi

  logger -p user.debug "dealing with compressed source ${compressed_source}"
  
  target_file="${TARGET_DIR}/${compressed_source}"
  make_containing_dir "${target_file}"

  # -h : check whether target exists and is symlink
  if [[ ! -h "${target_file}" ]] ; then
   ln -s "$(pwd)/${compressed_source}" "${target_file}" \
     || logger -p user.err "copying ${compressed_source} failed"
  fi

done

for uncompressed_source in **/*.flac ; do
  if [[ -d "${uncompressed_source}" ]]; then
    continue # skip directories that happen to have a matching suffix
  fi

  logger -p user.debug "dealing with uncompressed source ${compressed_source}"

  target_file="${TARGET_DIR}/${uncompressed_source%%.flac}.opus"
  #                                               ^ strip the .flac suffix
  make_containing_dir "${target_file}"

  #         /-- compare source file for "older than"
  #         |   target file; this returns !=0 if the source file
  #         |   is newer, or the target file nonexisting
  #         \--------------------\
  #                              |
  if [[ "${uncompressed_source}" -ot "${target_file}" ]]; then
    ffmpeg -loglevel=fatal \
           -i "${uncompressed_source}" \
           -b:a 96k \
           "${target_file}" \ 
      || logger -p user.err "transcoding ${uncompressed_source} failed"
  fi

done

这是非常未经测试的,但至少它会登录到syslog (journalctl -xef是您的朋友)

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/705840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档