FFmpeg - 2. deo
FFmpeg - napredne opcije
preuzeto odavde
Converting Audio into Different Formats / Sample Rates
Minimal example: transcode from MP3 to WMA:
You can get the list of supported formats with:
You can get the list of installed codecs with:
Convert WAV to MP3, mix down to mono (use 1 audio channel), set bit rate to 64 kbps and sample rate to 22050 Hz:
Convert any MP3 file to WAV 16khz mono 16bit:
Convert any MP3 file to WAV 20khz mono 16bit for ADDAC WAV Player:
cd into dir for batch process:
1for i in *.mp3; do ffmpeg -i "$i" -acodec pcm_s16le -ac 1 -ar 22050 "${i%.mp3}-encoded.wav"; done
Picking the 30 seconds fragment at an offset of 1 minute:
In seconds:
In HH:MM:SS format:
Split an audio stream at specified segment rate (e.g. 3 seconds)
Extract Audio:
- vn is no video.
- acodec copy says use the same audio stream that's already in there.
- The -i option in the above command is simple: it is the path to the input file.
- The second option -f mp3 tells ffmpeg that the ouput is in mp3 format.
- The third option i.e -ab 192000 tells ffmpeg that we want the output to be encoded at 192Kbps and -vn tells ffmpeg that we dont want video.
- The last param is the name of the output file.
Replace Audio on a Video without re-encoding
preferred method:
strip audio stream away from video
combine the two streams together (new audio with originally exisiting video)
or add an offset to audio
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:
To convert many files:
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.
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):
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:
A possible way to automatically parse the audio codec and name the audio file accordingly would be:
1for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".`ffprobe "$file" 2>&1 |sed -rn 's/.*Audio: (...), .*/\1/p'`; done
bashNote 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.
Encoding multiple files
You can use a Bash "for loop" to encode all files in a directory:
1mkdir newfiles
2for 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
A batch file version of the same command would be:
Extract Single Image from a Video at Specified Frame
- vf [ss][filename][outputFileName]
where vf is a custom bash script as follows:
- ss offset = frame number divided by FPS of video = the decimal (in milliseconds) ffmpeg needs i.e. 130.5
Merge Multiple Videos
concat demuxer
Split a Video into Images
Convert Images into a Video
1ffmpeg -f image2 -i image%d.jpg imagestovideo.mp4
2ffmpeg -i image-%03d.png -c:v libx264 -pix_fmt yuv420p test.mp4
3ffmpeg -r 1/5 -i image-%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p test.mp4
Convert Single Image into a Video
Convert non-sequentially named Images in a directory
Convert image sequence of many different sizes and conform to specific frame size
1$ ffmpeg -i image-%04d.jpg -c:v libx264 -pix_fmt yuv420p -vf "scale=max(1280\,a*720):max(1280\,720/a),crop=1280:720" test.mp4
Guarantee aspect ratio from image sequence
Evaluate which ratio to apply for scaling, then scale with the requisite amount of padding
1$ ffmpeg -i image-%04d.jpg -c:v libx264 -pix_fmt yuv420p -vf "scale=iw*min(1280/iw\,720/ih):ih*min(1280/iw\,720/ih), pad=1280:720:(1280-iw*min(1280/iw\,720/ih))/2:(720-ih*min(1280/iw\,720/ih))/2" test.mp4
1920 version:
1$ ffmpeg -i image-%04d.jpg -c:v libx264 -pix_fmt yuv420p -vf "scale=iw*min(1920/iw\,1080/ih):ih*min(1920/iw\,1080/ih), pad=1920:1080:(1920-iw*min(1920/iw\,1080/ih))/2:(1080-ih*min(1920/iw\,1080/ih))/2" test.mp4
Convert .mov (JPEG-A or other codec) to H264 .mp4
Simple FLAC convert
Mix Stereo to Mono
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.
Left channel to mono:
Left channel to stereo:
If you want to use the right channel, write 0.1.1 instead of 0.1.0.
Trim End of file (mp3)
Here's a command line that will slice to 30 seconds without transcoding:
To Encode or Re-encode ?
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]
use ffmpeg cut mp4 video without re-encoding
Example:
use ffmpeg cut mp4 video with re-encoding
Example:
If you want to cut off section from the beginning, simply drop -t 00:00:10 from the command
reduce filesize
Example:
It reduced a 100mb video to 9mb.. Very little change in video quality.
Example:
make a grayscale version and scale to 640x480
Convert MP4 to WEBM
Convert MKV to MP4
check for streams that you want (video/audio). be sure to convert/specify DTS 6 channel audio stream
1ffmpeg -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
Add Watermark overlay (png) to the center of a video
1ffmpeg -i source.mov -i watermark.png -filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" output.mp4
Concat a video with a reversed copy of itself for ping-pong looping effect
1ffmpeg -i input.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" output.mp4
more commands
http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs