在MacOS上使用ffmpeg将音频文件拆分为多个文件 [英] Split audio file into multiple files using ffmpeg on macOS

查看:74
本文介绍了在MacOS上使用ffmpeg将音频文件拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是节省一些时间,然后将长音频文件拆分为多个音频文件,然后从文本文件读取数据(startTime,endTime,艺术家,标题).
在MacOS(UNIX)和ffmpeg(音频)上使用终端.

My idea was to save some time and split long audio file into multiple audio files and reading the data (startTime, endTime, artist, title) from a text file.
Using terminal on MacOS (UNIX) and ffmpeg (audio).

三行输入的放置文件示例:

Input put file example with 3 lines:

00:00 - 06:16 - art - tit
06:16 - 09:22 - arti - titl
09:22 - 13:13 - artist - title

我的unix代码,外壳文件:"split.command"

My unix code, shell file: "split.command"

inputFileName=audiofile
input=test.txt


while IFS= read -r var
do

startTime=$(echo "$var" | cut -f1 -d"-")
endTime=$(echo "$var" | cut -f2 -d"-")
artist=$(echo "$var" | cut -f3 -d"-")
title=$(echo "$var" | cut -f4 -d"-")

echo ffmpeg -i ~/Downloads/$inputFileName.mp3 -metadata artist="$artist" -metadata title="$title" -ss $startTime -to $endTime -acodec copy "$title".mp3 -vsync 2

wait 

done < "$input"

当我回声它时,一切看起来都很好.

When I ECHO it, everything looks fine.

$ sh split.command
ffmpeg -i /Users/tony/Downloads/audiofile.mp3 -metadata artist= art  -metadata title= tit -ss 00:00 -to 06:16 -acodec copy  tit.mp3 -vsync 2
ffmpeg -i /Users/tony/Downloads/audiofile.mp3 -metadata artist= arti  -metadata title= titl -ss 06:16 -to 09:22 -acodec copy  titl.mp3 -vsync 2
ffmpeg -i /Users/tony/Downloads/audiofile.mp3 -metadata artist= artist  -metadata title= title -ss 09:22 -to 13:13 -acodec copy  title.mp3 -vsync 2

删除ECHO后,它应作为命令工作.第一个声音很好,第二个声音出错,第三个声音正常,但是缺少图稿.

When ECHO is deleted, it should work as command. The first audio is fine, the second throws an error, and the third work but has missing artwork .

1st *works*
2nd *error* Invalid duration specification for to: arti
3rd *works but..* [mp3 @ 0x7ff7e1800000] No packets were sent for some of the attached pictures.

推荐答案

有两个主要问题,第一个是读取文件时带有空格的问题,第二个是输出名称上带有空格的问题.在这里,它首先将所有属性转换为字符串,然后通过ffmpeg将其全部处理.

There were two major problems, the first with spaces when reading the file and the second was with the spaces at the output name. Here it first converts all the properties to a string and then it all proccess via ffmpeg.

read input        

inputFileName=$input
mkdir $input    
inputText=${inputFileName}.txt
i=0

function toString {
while IFS=- read -r startTime endTime artist title3; do
((i++))
title2="${title3// }"

str[i]=$(echo " -i "$PWD"/"${inputFileName}".mp3 -metadata artist="${artist// }" -metadata title="${title2// }" -ss "${startTime// }" -to "${endTime// }" -acodec copy "$input/${title2}".mp3 -vsync 2 ")
done< "$inputText"
}

function toSplit {
for j in $(seq 1 $i);
do
    ffmpeg ${str[$j]}
done
}

toString
toSplit

在macOS上,它仅在终端机上起作用:

On macOS it works only in terminal:

sh file.command

用作可执行文件时不起作用!

It does not work when used as executable file!

这篇关于在MacOS上使用ffmpeg将音频文件拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆