Convert Audio-Video files into Mp3 Audio and Mp4 Video using FFMPEG Commands

Just recently there was a requirement to convert several audio files extensions into Mp3 Audio and several video files extensions into Mp4 Video for one of the web application project.

 

Initially, searched to find best available multimedia frameworks to meet the requirement. But ultimately just got along with FFMPEG multimedia framework which provides command line tool to convert audio-video files extensions into Mp3 Audio and Mp4 Video.

 

The best command line tool I found to fit my requirement is FFMPEG. It is the command line multimedia framework which can be used to encode, decode, transcode, play etc. As FFMPEG suggest it run over Linux, windows and some other environments and configurations.
 
Mp4 Video Converter
 
It is a fast video and audio converter that can also grab from a live audio/video source. It can convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.
 
Now, let’s see further what commands I have used :
———————————————————————————————————-
  1. FFMPEG command to convert Audio files to mp3.
  • Single File Convert

Error when loading gists from https://gist.github.com/.ffmpeg -nostats -loglevel panic -y -i <inputfile>  -codec:a libmp3lame -qscale:a 3 <outputfile.mp3>

  • ​​Command Options
    • -nostats : This option will not print progress/statistics on console.
    • loglevel (panic|fatal|error|warning|info) :  This options only shows fatal errors which may crash the process.
    • -y : This option will overwrite output file without confirmation.
    • i : This option indicate the input file.
    • codec:a : This option indicate the audio codec. codec is the computer program for encoding or decoding the digital data stream or signal.
    • libmp3lame : To convert a file to mp3, you need to enable the LAME support. You can enable by this option.
    • qscale:a : This option is used for quality scale. a after(:) is stream_specifier.
 
  • Merge Multiple Files (Below shell script code is use to convert and merge multiple audio files in to one file [.mp3])

Error when loading gists from https://gist.github.com/.

for mfile in $ifiles
do
    outfile=${mfile%.*}
    ext=${mfile##*.}
    #convert to mp3 if file is already mp3
    if [ $ext != ‘mp3’ ]
    then
        _ffmpegcmd1="ffmpeg -nostats -loglevel panic -y -i $mfile -codec:a libmp3lame -qscale:a 3 "$outfile".mp3"
        $_ffmpegcmd1
    fi
    afile="$afile""$outfile"".mp3""$seperator"
done

ffmpeg -nostats -loglevel panic -y -i concat:$afile -acodec copy $ofile"_m.mp3"
ffmpeg -nostats -loglevel panic -y -i $ofile"_m.mp3" -vn -ar 44100 -ac 2 -ab 128k -f mp3 $ofile

———————————————————————————————————-

  1. FFMPEG command to convert Video files to mp4.
  • Single File Convert

Error when loading gists from https://gist.github.com/.ffmpeg -loglevel panic -y -i <inputfile> -deinterlace -pix_fmt yuv420p -an -vf scale=640:trunc(ow/a/2)*2 -vcodec libx264 -threads 0 <outputfile.mp4>

  • Command Options
    • -deinterlace : If you use this option then it may introduces some losses input file. 
    • pix_fmt : This refer pixel format.
    • yuv420p : This is most common format as data is more easily compressed.
    • an : This option is used to remove the audio from the output file.
    • scale : To scale output video.
    • libx264 : This is software lib to encoding streams
    • threads : No of thread to encode data. in this case its 0.

 

  • Merge Multiple Files (Below shell script code is use to convert and merge multiple files in to one file [.mp4])
Error when loading gists from https://gist.github.com/.

for mfile in $ifiles
do
    outfile=${mfile%.*}
    ext=${mfile##*.}
    #convert to mp4 if file is not mp4
    if [ $ext != ‘mp4’ ]
    then
        _ffmpegcmd1="ffmpeg -loglevel panic -y -i $mfile -deinterlace -pix_fmt yuv420p -an -vf scale=640:trunc(ow/a/2)*2 -vcodec libx264 -threads 0 "$outfile".mp4"
        $_ffmpegcmd1
    fi

    _ffmpegcmd2="ffmpeg -loglevel panic -y -i "$outfile".mp4 -deinterlace -pix_fmt yuv420p -an -vf scale=640:trunc(ow/a/2)*2 -c:v libx264 -r 60 -c:a aac -ar 48000 -b:a 160k -strict experimental -f mpegts "$outfile".ts"
    $_ffmpegcmd2

    vfile="$vfile""$outfile"".ts""$seperator"
done

ffmpeg -loglevel panic -y -i "concat:$vfile" -c copy -bsf:a aac_adtstoasc $ofile

———————————————————————————————————-

All the command here I used to process either Audio or Video files can be working fine to convert and to merge in extensions like (avi,mp4,mpeg,flv,wmv,3g2,3gp,mpg,mkv,mov,mod,mp3,wav,m4a,3gp,wma). 

 

Looking to explore more about FFMPEG, here is the download link to download FFMPEG. There are plenty of documentation resources available on site for multimedia command line tools.