Convert Your media files
Tags: ffmpeg, bash, linux
Published: 2019-06-27 09:48:42

---
title: Convert Your media files
published: True
description: convert video files to other formats
tags: ffmpeg,Bash,Linux
---

<h3>ffmpeg</h3>

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 <b>fast video and audio converter</b>
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

<b>apt-get install ffmpeg</b>

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

<b>NOTE: ffmpeg is a command line program.</b>

<b>ffmpeg -i input.mp4 -vn output.mp3</b>

-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
<b>ffmpeg -i input.mp4 -sn output.mp4</b>
if the input had subtitles enabled the output will be free of subtitle

<b>ffmpeg -i input.mp4 -an output.mp4</b>

will result into a video with no sound


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

<b>ffmpeg -i input.flv -q:a 0 -q:v 0  -f avi output.avi</b>

-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


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

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
<b>ffplay -i input</b>

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 <b><a>https://github.com/peter-macharia/media-converter</a> </b>