使用bash循环浏览目录中的文件,然后根据文件名创建子目录

时间:2018-12-05 19:48:23

标签: bash ffmpeg

我想做的是使用bash

1) cycle through files in a directory 
2) then create a sub-directory based on the file names 
3) then split mp3 files using ffmpeg in 3 second increments.

我可以创建子目录,ffmpeg代码使用正确的名称拆分文件。我似乎遇到的问题是循环。它会创建split-chirp目录及其所有文件,但不会处理 pink.mp3 文件。参见起始目录下图 Start directory

查看结束目录的图像 Ending directory

它不会创建粉红色的目录和所有文件(我知道这是一个循环问题,只是无法弄清原因)

我使用命令bash mp3spl.sh *.mp3

运行代码

下面的代码

#!/bin/bash
currentdir=$(pwd) #get current directory

for f in $currentdir/*.mp3
do

fn=`echo "$1" | cut -d'.' -f1` #get just the filename no extension
splitdirname="$currentdir/split-$fn"  #sub directory with correct names 
mkdir -p "$splitdirname" #make split directory
echo "Processing $f"
ffmpeg -i "$1" 2> tmp.txt

    ffmpeg -i "$1" -f segment -segment_time 3 -ar 22050 -ac 1 "$splitdirname/$fn-%03d.mp3"

#rm tmp.txt

done

2 个答案:

答案 0 :(得分:0)

$1是不必要的,因为您希望循环播放所有mp3文件。

fn=$(basename "$f" | cut -d'.' -f1) #get just the filename no extension

其余的都没动。

答案 1 :(得分:0)

这将与目录和文件名中的空格一起使用

#!/bin/bash
#run using bash mp3spl.sh
currentdir="$(pwd)" #get current directory


for f in *.mp3   #only look for mp3 files
do

fn=$(basename "$f" | cut -d'.' -f1) #get just the filename no extension

splitdirname="$currentdir/split-$fn" #sub directory with correct names
mkdir -p "$splitdirname" #make split directory
#echo "Processing $f"
ffmpeg -i "$1" 2> tmp.txt

ffmpeg -i "$f" -f segment -segment_time 1200 -ar 22050 -ac 1 "$splitdirname/%03d-$fn.mp3" #split every 20mins

#rm tmp.txt

done