我这里提供一个简单的bash脚本,轻松的在Linux系统中将mp4转换为mp3,该脚本可将mp4、mkv和webm视频格式转换为mp3音频格式。前提条件是有FFmpeg及Lame,对于Arch Linux用户要安装基本条件,运行命令pacman -S lame ffmpeg。
在Linux中将mp4转换为mp3 1、从github下载脚本,脚本地址:https://github.com/NerdJK23/dotfiles/blob/master/scripts/mp3con.sh 也可以复制下面的内容到脚本下面: #!/usr/bin/env bash # My bash Script to convert mp4 to mp3 # By NerdJK23 # Requires # ffmpeg installed # lame installed echo -ne """ 1: Current directory 2: Provide directory """ echo "" echo -n "Selection : " read selection case $selection in 1) echo "Okay.." echo "" echo "Current dir is `pwd` " ;; 2) echo "" echo -n "Give diretory name: " read dir_name # Check if given directory is valid if [ -d $dir_name ]; then cd "${$dir_name}" echo "Current directory is `pwd` " echo else echo "Invalid directory, exiting.." echo "" exit 10 fi echo ;; *) echo echo "Wrong selection" exit 11 ;; esac echo "" # Create dir to store mp3 files if it doesn't exist # First get the current directory name current_dir=`pwd` base_name=` basename "$current_dir"` if [[ ! -d "$base_name"-mp3 ]]; then echo "$base_name" | xargs -d " " -I {} mkdir {}-mp3 echo "" fi echo "" # Bigin to covert videos to mp3 audio files # -d " " > Change delimiter from any whitespace to end of line character find . -name "*.mp4" -o -name "*.mkv" -o -name "*.webm" | xargs -d " " -I {} ffmpeg -i {} -b:a 320K -vn "$base_name"-mp3/{}.mp3 # remove video extensions cd "${base_name}"-mp3 for file_name in *; do mv "$file_name" "`echo $file_name | sed "s/.mp4//g;s/.mkv//g;s/.webm//g"`"; done # Move audio directory to ~/Music if [[ ! -d ~/Music ]]; then mkdir ~/Music fi cd .. mv "$base_name"-mp3 ~/Music/ # Check if conversion successfull echo "" if [[ $? -eq "0" ]];then echo " All files converted successfully" else echo "Conversation failed" exit 1 fi 2、使脚本可执行: # chmod +x mp3con.sh 3、执行脚本(你可以在包含mp4视频的文件夹中执行): # ./mp3con.sh 或者: # sh mp3con.sh 4、你会看到类似下图的画面:
5、由于我在带有视频文件的目录中运行脚本,我想转换为mp3,我将选择1,按Enter键等待它完成。 6、转换成功后,生成的音频文件将保存在~/Music目录下,文件夹名称为包含视频文件的上一个文件夹的名称,末尾带有扩展名-mp3。如果在~/Music目录中执行ls,则应该看到添加了新文件夹。
相关主题 |