DEV Community

c3phas
c3phas

Posted on

Convert Your media files

ffmpeg

So you have the video but can’t play it ‘cos of the format ?
You want an mp3 but have got the mp4 or other video format ?
You want the Video without the sound
You have an image that should be the background of your mp3 file

ffmpeg according to the man page is a fast video and audio converter
ffmpeg library is cross platform thus can be used in any operating system ,
ffmpeg can be used for a torn of things but we only look at the basic here, This library is used in the background by famous video editors .

Install ffmpeg on debian based system

apt-get install ffmpeg

Lets begin by converting a video(mp4 to an mp3)

NOTE: ffmpeg is a command line program.

ffmpeg -i input.mp4 -vn output.mp3

-i is used to specify the input file , the file can be an mp4 ,webm or other video formats
-vn – this option is used to disable the video recording. It grabs the video and gets rid of it
output.mp3 is the output file name

The above command will convert the input.mp4 to output.mp3
Other related options include
-sn – disable the subtitle
-an -disable the audio recording

examples
ffmpeg -i input.mp4 -sn output.mp4
if the input had subtitles enabled the output will be free of subtitle

ffmpeg -i input.mp4 -an output.mp4

will result into a video with no sound

Convert a video from one format to another( say from flv to avi)

ffmpeg -i input.flv -q:a 0 -q:v 0 -f avi output.avi

-i specify the input file
-q used to set the quality of the audio and video file , a is for audio and v is for video
-q:a 0 means the output audio quality should be the same as the input audio quality(mainly called streams
-f -is used to specify the target format in this case we want an avi output

note The only thing you need to change are the input format and output format

Add an image to the mp3 file
so you have an mp3 but want to use your pic instead of staring at the bare screen
ffmpeg -loop 1 -y -i input.jpg -i input.mp3 -acodec flac -vcodec libX264 -shortest -preset fast vf scale=1280:2 output.mkv

well well now we get a bit advanced here
we have some more options lets look at some of them

codecs – This simply refers to the media bitstream( How the media will be encoded and decoded)
acodec is used to set the audio bit stream in our case we use flac
vcodec sets the video codec
-y if a file with same name as the output name is present it will be deleted ie overwrite the output without asking ( -n is the opposite , says do not overwrite )
-loop 1 - will loop our image until the mp3 finishes
-shortest – Finish encoding when the shortest input stream ends
-preset fast – how fast will the compression process take. Can be set to fast,slow ,very slow etc
when the value is set to fast , The quality of the output might be low but the process will end quickly. Its a trade off speed vs quality
The next vf is used to set the video format in this case we set the output video size using the scale option , The output video will be 1280 wide

NOTE: we have specified two input files , an image and an audio file
To suppress the output add the option ( -hide_banner)

if you wish to just preview how your output will be , use ffplay to play the file without saving it
ffplay -i input

Ffmpeg can be used for far more advanced things which is impossible to cover.. Check the ffmpeg documentation
To get a more clear bash script visit my github https://github.com/peter-macharia/media-converter

Top comments (4)

Collapse
 
kromiro1 profile image
Kromiro

Is there an easy or ideally free way to convert .wmv video files to .mp4 files so they can be viewed on a mac? I used Aiseesoft but the free trial only lets you convert up to 5 minutes of video.

Collapse
 
c3phas profile image
c3phas

Hey Kromiro , If you have ffmpeg installed just run the following:

ffmpeg -i video.wmv -q:a 1 -q:v 1 -f mp4 output.mp4

Collapse
 
kromiro1 profile image
Kromiro

Thank!

Collapse
 
c3phas profile image
c3phas

Install ffmpeg on mac by typing

brew install ffmpeg