gpt4 book ai didi

将音乐从一个目录转换到另一个目录的 Bash 脚本

转载 作者:行者123 更新时间:2023-12-04 22:56:19 25 4
gpt4 key购买 nike

我已经从 Arch 论坛修改了这个脚本:https://wiki.archlinux.org/index.php/Convert_Flac_to_Mp3#With_FFmpeg

我试图在目录结构中找到特定的文件类型,将它们转换为另一种音乐文件类型,并将它们放在保持相同目录结构的“转换”目录中。

我被困在剥离字符串 $b的文件名。
$b保存字符串 ./converted/alt-j/2012\ an\ awesome\ wave/01\ Intro.flac
有没有办法可以从字符串中删除文件名?我不认为 ffmpeg 可以创建/强制输出文件的父目录。

#!/bin/bash
# file convert script
find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
b=${a/.\//.\/converted/}
< /dev/null ffmpeg -i "$a" "${b[@]/%flac/ogg}"
#echo "${b[@]/%flac/ogg}"

最佳答案

I'm stuck at stripping the string $b of its file name.



让我们从 b 开始:
$ b=./converted/alt-j/2012\ an\ awesome\ wave/01\ Intro.flac

要删除文件名,请保留路径:
$ c=${b%/*}

要验证结果:
$ echo "$c"
./converted/alt-j/2012 an awesome wave

确保目录 c存在,做:
$ mkdir -p "$c"

或者,一步完成:
$ mkdir -p "${b%/*}"

这个怎么运作

我们正在使用 shell 的后缀删除功能。形式为 ${parameter%word} ,shell 找到 word 的最短匹配反对结束 parameter并将其删除。 (注意 word 是一个 shell glob,而不是一个正则表达式。)在这种情况下, word/*匹配斜杠后跟任何字符。因为这会删除最短的匹配项,所以只会从参数中删除文件名部分。

后缀去除详细文档

来自 man bash :

${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the %'' case) or the longest matching pattern (the%%'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

关于将音乐从一个目录转换到另一个目录的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com