如何使用ffmpeg将MP4/MKV/WEBM转换为MP3上的MP3
时间:2020-02-23 14:32:39 来源:igfitidea点击:
在某些时候的每个人都想要或者希望将MP4/MPV/WebM文件转换为Linux上的MP3.
如果我们想在任何Linux系统上实现这一点,请不要担心,因为我对我们有一个很好的解决方案。
FFMPEG允许我们将任何媒体文件从一种格式转换为另一个格式。
我们有一个简单的bash脚本,可用于将MP4/MKV/WEBM媒体文件转换为Linux上的MP3.
脚本可以在几秒钟内将MP4,MKV和WebM视频格式转换为MP3音频格式。
准备工作
ffmpeglame.
我们将有一个小分配,了解如何获得CFMPEG和跛脚的分发。
但对于常见的Linux发行版,请检查下一节。
第1步:安装ffmpeg
对于ARCH Linux用户,安装准备工作; ffmpeg和lame by:
sudo pacman -S lame ffmpeg
对于ubuntu/debian:
sudo apt -y install ffmpeg lame
对于Fedora:
sudo dnf -y install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm sudo dnf -y install https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm sudo dnf -y install ffmpeg ffmpeg-devel lame
对于CentOS 7.
sudo yum -y install epel-release sudo rpm -v --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm sudo yum install ffmpeg ffmpeg-devel lame
CentOS/RHEL 8:在CentOS/RHEL 8上安装FFMPEG
还可以通过手动编译源的FFMPEG来完成安装。
第2步:下载转换脚本
从github下载脚本
wget https://raw.githubusercontent.com/jmutai/dotfiles/master/scripts/mp3con.sh
我们可以在脚本片段下面复制。
#!/usr/bin/env bash
# My bash Script to convert mp4 to mp3
# By Josphat Mutai
# web: www.theitroad.com
# email: Hyman@theitroad
# Requires
# ffmpeg installed
# lame installed
# Check https://theitroad.com/how-to-convert-mp4-to-mp3-on-linux/
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 "\n" -I {} mkdir {}-mp3
echo ""
fi
echo ""
# Bigin to covert videos to mp3 audio files
# -d "\n" > Change delimiter from any whitespace to end of line character
find . -name "*.mp4" -o -name "*.mkv" -o -name "*.webm" | xargs -d "\n" -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
使脚本可执行文件:
chmod +x mp3con.sh
第3步:将MP4/MKV/WEBM媒体文件转换为Linux上的MP3:
执行脚本,(我们可以在包含MP4视频的文件夹中执行此操作)
./mp3con.sh
要不就
sh mp3con.sh
你会看到类似于下面的东西
由于我在带有视频文件的目录内运行脚本,我想转换为MP3,我会选择选项1.按Enter并等待它完成。
成功转换后,生成的音频文件将保存在~/music目录下,其中文件夹名称是包含extentsion -mp3的视频文件的上一个文件夹的名称。
$ls ~/Music/Gospel-mp3 'Broken Vessels (Amazing Grace) Hillsong UNITED from the Sea of Galilee-MlPuR-0XxiA.mp3' 'Hillsong United - '\''Oceans'\'' (Live show at Caesarea)-HVAR85rorvU.mp3' 'Hosanna - From HOLY LAND in Jerusalem - Hillsong United-mqF1EhsBz0A.mp3' 'Jonathan Nelson - I Believe (Island Medley) (LIVE)-vDLByAnQ93Q.mp3' 'No Longer Slaves (Official Lyric Video) - Jonathan David & Melissa Helser - W....mp3' 'Travis Greene - Intentional (Official Music Video).mp3'

