Friday, August 30, 2019

A guide to ffmpeg



https://www.tecmint.com/ffmpeg-commands-for-video-audio-and-image-conversion-in-linux/
12. Increase/Reduce Video Playback Speed
To increase video play back speed, run this command. The -vf option sets the video filters that helps to adjust the speed.

$ ffmpeg -i video.mpg -vf "setpts=0.5*PTS" highspeed.mpg

ou can also reduce video speed as follows:

$ ffmpeg -i video.mpg -vf "setpts=4.0*PTS" lowerspeed.mpg -hide_banner

https://opensource.com/article/17/6/ffmpeg-convert-media-file-formats
FFmpeg to the rescue! You can select the codecs needed by using the -c flag.
This flag lets you set the different codec to use for each stream. For example, to set the audio stream to be Vorbis, you would use the following command:
ffmpeg -i input.mp3 -c:a libvorbis output.ogg
The same can be done to change the video as well as the audio stream:
ffmpeg -i input.mp4 -c:v vp9 -c:a libvorbis output.mkv
This will make a Matroska container with a VP9 video stream and a Vorbis audio stream, essentially the same as the WebM we made earlier.
The command ffmpeg -codecs will print every codec FFmpeg knows about.

Changing a single stream

More often than you'd like, the file you have is partially correct with only a single stream in the wrong format. It can be very time consuming to re-encode the correct stream. FFmpeg can help with this situation:
ffmpeg -i input.webm -c:v copy -c:a flac output.mkv
This command copies the video stream from input.webm into output.mkv and encodes the Vorbis audio stream into a FLAC. The -c flag is really powerful.

Changing a container

The prior example can be applied to both the audio and video streams, allowing you to convert from one container format to another without having to do any additional stream encoding:
ffmpeg -i input.webm -c:av copy output.mkv
Change the quality
ffmpeg -i input.webm -c:a copy -c:v vp9 -b:v 1M output.mkv
This will copy the audio (-c:a copy) from input.webm and convert the video to a VP9 codec (-c:v vp9) with a bit rate of 1M/s (-b:v), all bundled up in a Matroska container (output.mkv).
Another way we can impact quality is to adjust the frame rate of the video using the -roption:
ffmpeg -i input.webm -c:a copy -c:v vp9 -r 30 output.mkv
This creates a new Matroska with the audio stream copied over and the video stream's frame rate forced to 30 frames per second, instead of using the frame rate from the input (-r 30).
You can also adjust the dimensions of your video using FFmpeg. The simplest way is to use a predetermined video size:
ffmpeg -i input.mkv -c:a copy -s hd720 output.mkv
This modifies the video to 1280x720 in the output, but you can set the width and height manually if you want:
ffmpeg -i input.mkv -c:a copy -s 1280x720 output.mkv
This produces the exact same output as the earlier command. If you want to set custom sizes in FFmpeg, please remember that the width parameter (1280) comes before height (720).






https://catswhocode.com/ffmpeg-commands/

Get File Information From a Video File
ffmpeg -i video.avi

Convert Images To a Video Sequence
This command will transform all the images from the current directory (named image1.jpg, image2.jpg, etc…) to a video file named video.mpg.

ffmpeg -f image2 -i image%d.jpg video.mpg
Convert a Video to X Images
This command will generate imagess named image1.jpg, image2.jpg, etc, from a given video file. The following image formats are available: PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

ffmpeg -i video.mpg image%d.jpg
Crop a Video File
Cropping is a very common operation in video editing. FFmpeg provides a crop filter for this specific purpose:

ffmpeg -i input.mp4 -filter:v "crop=out_w:out_h:x:y" output.mp4
The options are as follows:

out_w is the width of the output rectangle
out_h is the height of the output rectangle
x and y specify the top left corner of the output rectangle
output.mp4 is the output file
Resize a Video
Using the -vf scale filter, it is possible to resize videos to a desired size:

ffmpeg -i input.avi -vf scale=320:240 output.avi
The same works with images as well:

ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png
Extract a Portion of a Video
Another very common operation on video files is to extract a specific portion of a given video. This can be done super easily:

ffmpeg -ss 00:00:30 -i orginalfile.mpg -t 00:00:05 -vcodec copy -acodec copy newfile.mpg
In the example above, we are cutting out a part starting at 00:00:30 into the original file with a 5 seconds length. -ss indicates the starting time, and -t indicates the duration.

Encode a Video Sequence for the iPod/iPhone
You can easily convert a video for iPhones and older iPods using this command:

ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
Explanations :

Source : source_video.avi
Audio codec : aac
Audio bitrate : 128kb/s
Video codec : mpeg4
Video bitrate : 1200kb/s
Video size : 320px par 180px
Generated video : final_video.mp4

Extract Sound From a Video, And Save It in Mp3 Format
Creating an audio file from a video is an easy task:

ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 sound.mp3
Explanations :

Source video : source_video.avi
Audio bitrate : 192kb/s
output: mp3 format
Generated sound : sound.mp3
Convert a Wav File to Mp3
FFmpeg isn’t only for videos, there’s a lot you can do with audio files as well. This example will convert a .wav file to mp3 format.

ffmpeg -i input_sound.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 output_sound.mp3


Image Overlay on a Video
Let’s finish this round-up with an advanced command. Here we are applying an overlay image to an existing video:

ffmpeg -i input.mp4 -i image.png -filter_complex "[0:v][1:v] overlay=25:25:enable='between(t,0,20)'" -pix_fmt yuv420p -c:a copy output.mp4
Some explanations:

overlay=25:25: The image will be positioned 25px to the right and 25px down, originating from the top left corner (0:0).
enable='between(t,0,20)': The overlay image will be shown from 00:00:00 to 00:00:20
Youtube-dl is a nifty little tool you can use to grab videos from YouTube and several other video-streaming services. It's super straightforward to use:
youtube-dl https://www.youtube.com/watch?v=2m5nW9CQLJ0
https://medium.com/@dernis/ffmpeg-tips-tricks-and-and-lessons-learned-a6f3c1187085
https://gist.github.com/revolunet/57a84ae4353615561b0a



https://www.ffmpeg.org/
https://www.ffmpeg.org/ffmpeg.html
As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times. Each occurrence is then applied to the next input or output file. Exceptions from this rule are the global options (e.g. verbosity level), which should be specified first.
Do not mix input and output files – first specify all input files, then all output files. Also do not mix options which belong to different files. All options apply ONLY to the next input or output file and are reset between files.
  • To set the video bitrate of the output file to 64 kbit/s:
    ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
    
  • To force the frame rate of the output file to 24 fps:
    ffmpeg -i input.avi -r 24 output.avi
    
  • To force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:
    ffmpeg -r 1 -i input.m2v -r 24 output.avi
    


https://unix.stackexchange.com/questions/28803/how-can-i-reduce-a-videos-size-with-ffmpeg
Calculate the bitrate you need by dividing 1 GB by the video length in seconds. So, for a video of length 16:40 (1000 seconds), use a bitrate of 1000000 bytes/sec:
ffmpeg -i input.mp4 -b 1000000 output.mp4
Additional options that might be worth considering is setting the Constant Rate Factor, which lowers the average bit rate, but retains better quality. Vary the CRF between around 18 and 24 — the lower, the higher the bitrate.
ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4

Second command, using -crf 24 took a 255.3MB video I had and reduced it to 72.7MB without lowering the quality noticeably.
https://tutorials.technology/tutorials/52-How-to-convert-mkv-to-mp4-on-macOS.html
brew install ffmpeg
for f in *.mkv;do ffmpeg -i "$f" -c:v copy -c:a aac -b:a 256k -strict -2 "${f%mkv}mp4";done
https://unix.stackexchange.com/questions/35746/encode-with-ffmpeg-using-avi-to-mp4
for f in *.avi;do ffmpeg -i "$f"  -strict -2 "${f%avi}mp4";done
The command -strict -2 is necessitated by the AAC codec which is experimental, but works (libaac), if you add those two parameters. The output file is high-quality by default.
ffmpeg -i input.avi -strict -2 output.mp4
https://unix.stackexchange.com/questions/28803/how-can-i-reduce-a-videos-size-with-ffmpeg


https://askubuntu.com/questions/19322/how-to-convert-rmvb-to-mp4-or-why-ffmpeg-doesnt-work


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts