ffmpeg -i input.avi -s 720x480 -c:a copy output.mkv
scale and filter provides more flexibility:
ffmpeg -i input.avi -filter:v scale=720:-1 -c:a copy output.mkv
The -1 will tell ffmpeg to automatically choose the correct height in relation to the provided width to
preserve the aspect ratio. -1 can also be used for width if you provide a given height.
One downside of scale when using libx264 is that this encoder requires even values and scale may automatically
choose an odd value resulting in an error: width or height not divisible by 2. You can tell scale to choose
an even value for a given height (e.g. 720):
scale="trunc(oh*a/2)*2:720"
or a given width (e.g. 1280):
scale="1280:trunc(ow/a/2)*2"
If your ffmpeg build complains about not recognizing -c or -filter options, or doesn't support scale,
you may need to install a more recent ffmpeg
$ ffmpeg -i source-file.foo -ss 0 -t 600 first-10-min.m4v
$ ffmpeg -i source-file.foo -ss 600 -t 600 second-10-min.m4v
$ ffmpeg -i source-file.foo -ss 1200 -t 600 third-10-min.m4v
where:
-ss - start time in secs
-t - length of chunk (e.g. 1 min = 60, 10 mins = 600)
-i - input file
# Scale to a width of 480, start at 10 minute mark and extract just a 2 minute video
ffmpeg -i input.mp4 -filter:v scale=480:-1 -c:a copy -ss 600 -t 120 output.mp4
$ ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4
# or
$ ffmpeg -i input.mp4 -vcodec h264 -acodec aac output.mp4
a. Minimal example: transcode from MP3 to WMA:
ffmpeg -i input.mp3 output.wma
You can get the list of supported formats with:
ffmpeg -formats
b. Convert WAV to MP3, mix down to mono (use 1 audio channel), set bit rate to 64 kbps and sample rate to 22050 Hz:
ffmpeg -i input.wav -ac 1 -ab 64000 -ar 22050 output.mp3
c. Convert any MP3 file to WAV 16khz mono 16bit:
ffmpeg -i 111.mp3 -acodec pcm_s16le -ac 1 -ar 16000 out.wav
d. Convert any MP3 file to WAV 20khz mono 16bit for ADDAC WAV Player:
ffmpeg -i 111.mp3 -acodec pcm_s16le -ac 1 -ar 22050 out.wav
e. cd into dir for batch process:
for i in *.mp3; do ffmpeg -i "$i" -acodec pcm_s16le -ac 1 -ar 22050 "${i%.mp3}-encoded.wav"; done
f. Picking the 30 seconds fragment at an offset of 1 minute:
In seconds
ffmpeg -i input.mp3 -ss 60 -t 30 output.wav
In HH:MM:SS format
ffmpeg -i input.mp3 -ss 0:01:00 -t 0:00:30 output.wav
ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac
vn - no video.
acodec copy - use the same audio stream that's already in there.
ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3
where:
-i - path to the input file.
-f mp3 - the output should be in mp3 format.
-ab 192000 - the output should be encoded at 192Kbps, and
-vn - we don't want video.
The last param is the name of the output file.
b. Replace Audio on a Video without re-encoding. Strip audio stream away from video
ffmpeg -i INPUT.mp4 -codec copy -an OUTPUT.mp4
c. Combine the two streams together (new audio with originally exisiting video)
ffmpeg -i INPUT.mp4 -i AUDIO.wav -shortest -c:v copy -c:a aac -b:a 256k OUTPUT.mp4
d. You say you want to "extract audio from them (mp3 or ogg)". But what if the audio in the mp4 file is not one of those? you'd have to transcode anyway. So why not leave the audio format detection up to ffmpeg?
To convert one file:
ffmpeg -i videofile.mp4 -vn -acodec libvorbis audiofile.ogg
To convert many files:
for vid in *.mp4; do ffmpeg -i "$vid" -vn -acodec libvorbis "${vid%.mp4}.ogg"; done
You can of course select any ffmpeg parameters for audio encoding that you like, to set things like bitrate and so on.
Use -acodec libmp3lame and change the extension from .ogg to .mp3 for mp3 encoding.
e. If what you want is to really extract the audio, you can simply "copy" the audio track to a file using -acodec copy. Of course, the main difference is that transcoding is slow and cpu-intensive, while copying is really quick as you're just moving bytes from one file to another. Here's how to copy just the audio track (assuming it's in mp3 format):
ffmpeg -i videofile.mp4 -vn -acodec copy audiofile.mp3
Note that in this case, the audiofile format has to be consistent with what the container has (i.e. if the audio is AAC format, you have to say audiofile.aac). You can use the ffprobe command to see which formats you have, this may provide some information:
for file in *; do ffprobe $file 2>&1 |grep Audio; done
f. A possible way to automatically parse the audio codec and name the audio file accordingly would be:
for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".`ffprobe "$file" 2>&1 |sed -rn 's/.*Audio: (...), .*/\1/p'`; done
Note that this command uses sed to parse output from ffprobe for each file, it assumes a 3-letter audio codec name (e.g. mp3, ogg, aac) and will break with anything different.
g. Encoding multiple files
You can use a Bash "for loop" to encode all files in a directory:
$ mkdir newfiles
$ for f in \*.m4a; do ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done
m4a to mp3 conversion with ffmpeg and lame
ffmpeg -i input.m4a -acodec libmp3lame -ab 128k output.mp3
A batch file version of the same command would be:
for f in *.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 256k "${f%.m4a}.mp3"; done
h. Extract Single Image from a Video at Specified Frame
$ vf [ss][filename][outputFileName]
where vf is a custom bash script as follows:
$ ffmpeg -ss $1 -i $2 -qmin 1 -q:v 1 -qscale:v 2 -frames:v 1 -huffman optimal $3.jpg
where
ss offset = frame number divided by FPS of video = the decimal (in milliseconds) ffmpeg needs i.e. 130.5
N.B: File names in folder, if they contain spaces, must be properly escaped
ls * | perl -ne 'print "file $_"' | ffmpeg -f concat -i - -c copy merged.mp4
$ ffmpeg -i video.flv image%d.jpg
$ ffmpeg -f image2 -i image%d.jpg imagestovideo.mpg
$ ffmpeg -i example.mp4 -f webm -c:v libvpx -b:v 1M -acodec libvorbis example.webm -hide_banner
ffmpeg -i audio.xxx -c:a flac audio.flac
You can modify a video file directly without having to re-encode the video stream. However the audio stream will have to be re-encoded.
a. Left channel to mono:
ffmpeg -i video.mp4 -map_channel 0.1.0 -c:v copy mono.mp4
b. Left channel to stereo:
ffmpeg -i video.mp4 -map_channel 0.1.0 -map_channel 0.1.0 -c:v copy stereo.mp4
If you want to use the right channel, write 0.1.1 instead of 0.1.0.
Here's a command line that will slice to 30 seconds without transcoding:
ffmpeg -t 30 -i inputfile.mp3 -acodec copy outputfile.mp3
Do you need to cut video with re-encoding or without re-encoding mode? You can try to following below command:
Synopsis:
ffmpeg -i [input_file] -ss [start_seconds] -t [duration_seconds] [output_file]
ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -c copy cut_video.mp4
ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -async 1 -strict -2 cut_video.mp4
If you want to cut off section from the beginning, simply drop -t 00:00:10 from the command.
ffmpeg -i input.avi -vcodec libx264 -crf 24 output.avi
It reduced a 100mb video to 9mb. Very little change in video quality.
ffmpeg -i video.mov -vf eq=saturation=0 -s 640x480 -c:v libx264 -crf 24 output.mp4
ffmpeg -i file.mkv
Check for streams that you want (video/audio), and be sure to convert/specify DTS 6 channel audio stream.
ffmpeg -i input.mkv -strict experimental -map 0:0 -map 0:1 -c:v copy -c:a:1 libmp3lame -b:a 192k -ac 6 output.mp4
a. Default (With no [STREAM] wrapper)
```
$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=nw=1 input.mp4
width=1280
height=720
```
With no key:
```
$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=nw=1:nk=1 input.mp4
1280
720
```
b. CSV
```
$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 input.mp4
1280,720
```
c. JSON
```
$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json input.mp4
{
"programs": [
],
"streams": [
{
"width": 1280,
"height": 720
}
]
}
```
d. XML
```
$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of xml input.mp4
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
<programs>
</programs>
<streams>
<stream width="1280" height="720"/>
</streams>
</ffprobe>
```
Note: See -of option documentation for more choices and options. Also see FFprobe Tips for other examples including duration and frame rate.
More commands can be found at Cats Who Code
There are a few ways to count the frames in a video using ffmpeg:
ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 input.mp4ffmpeg -i input.mp4 -map 0:v:0 -c copy -f null -This will output something like "frame= 1234" at the end of processing.
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 input.mp4The first method (using ffprobe) is generally the quickest and most straightforward. Just replace
input.mp4with your video filename.
If you want to get frame count along with frame rate and duration, you can use:
ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames,r_frame_rate,duration -of default=noprint_wrappers=1 input.mp4
Thanks, this is very useful.
What is the recommended way to convert a DTS audio file to a web-compatible format, while preserving the highest possible quality?