Showing posts with label video editing. Show all posts
Showing posts with label video editing. Show all posts

Thursday, November 30, 2023

Reducing a video's size with FFMPEG

 The following command uses the H.265 codec that compresses more for the same quality:

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

Other options for further compression are the following:

To scale to half size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx265 -crf 28 half_the_frame_size.mkv

One-third size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/6)*2:trunc(ih/6)*2" -c:v libx265 -crf 28 a_third_the_frame_size.mkv

One-quarter size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/8)*2:trunc(ih/8)*2" -c:v libx265 -crf 28 a_fourth_the_frame_size.mkv

One-fifth size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/10)*2:trunc(ih/10)*2" -c:v libx265 -crf 28 a_fifth_the_frame_size.mkv

In these examples, the size is divided by twice of the reduction value and multiplied by two to ensure the pixel size is a multiple of two, which is required for some codecs, including H265. Changing the resolution always requires re-encoding.



Sunday, December 26, 2021

CONCATENATION OF VIDEOS [CONCATENATE]

FFmpeg has three concatenation methods:


1. concat video filter

Use this method if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering.

Note that this method performs a re-encode of all inputs. If you want to avoid the re-encode, you could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding everything.

ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \

-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \

concat=n=3:v=1:a=1 [v] [a]" \

-map "[v]" -map "[a]" output.mkv


2. concat demuxer

Use this method when you want to avoid a re-encode and your format does not support file-level concatenation (most files used by general users do not support file-level concatenation).

$ cat mylist.txt

file '/path/to/file1'

file '/path/to/file2'

file '/path/to/file3'

$ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4


3. concat protocol

Use this method with formats that support file-level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.

$ ffmpeg -i "concat:input1|input2" -codec copy output.mkv

This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method.

--

If in doubt about which method to use, try the concat demuxer.

---

Source: Stackoverflow.


Sunday, September 20, 2020

Commands useful along with Youtube-dl

The following is to download a only a particular part of a video from YouTube 

ffmpeg -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=cmne_rokrs4) -ss 00:10:10 -to 00:12:52 -c:v copy -c:a copy happy.mp4

Or you can download all the video first and then cut some parts following the instructions here.

If you want to concatenate MP4 videos (you can prepare an script):

echo file input1.mp4 >  mylist.txt

echo file input2.mp4 >>  mylist.txt

ffmpeg -f concat -i mylist.txt -c copy output.mp4

To list all subs for a video:

youtube-dl --list-subs URL

To download all subs, but not the video:

youtube-dl --all-subs --skip-download URL

Or you can only download one subtitle

youtube-dl --write-sub --sub-lang en --skip-download URL

Or  for downloading the automatically generated subtitles

youtube-dl --write-auto-sub --skip-download URL

If you want to convert the subtitles do:

ffmpeg -i input.vtt output.srt

The following is to convert videos to a higher speed. For instance, if you want to multiply by 1.15, the command is:

ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.87*PTS[v];[0:a]atempo=1.15[a]" -map "[v]" -map "[a]" output.mkv

#The "setpts" corresponds to the inversed speed (in this case 100/1.25 = 0.8)

#while the "atempo" corresponds to the desired direct speed of the video.

#Those values are proportionally inversed.

#For a 25% higher speed output video the values

#would be "setpts=0.8*PTS" and "atempo=1.25"

100/1.25 = 0.8

References:

Wednesday, November 01, 2017

Cutting Video with ffmpeg

The highlighted represent the starting point for the cut. In this case no ending point set, so by default the output will include all the remaining of the video.

ffmpeg -i input.mp4 -ss 00:20:01 -async 1 -c copy output.mp4 

If we want to cut the video before the end we cand do it in two ways:

ffmpeg -i input.mp4 -ss 00:20:03 -t 00:01:08 -async 1 -c copy output.mp4

The "-t" option specifies a duration, not an end time. The above command will encode 1 minute and 8 seconds of video starting from the time 00:20:01

ffmpeg -i input.mp4 -ss 00:20:03 -to 00:21:11 -async 1 -c copy output.mp4

If you are using a current version of ffmpeg you can also replace "-t" with "-to" in the above command to end at the specified time.

Note:
Use "-c copy" for making it instantly. In that case ffmpeg will not re-encode the video, just will cut to according size.


-----///

Thursday, October 08, 2015

"ffmpeg" instead of "avconv" in youtube-dl

youtube-dl  https://www.youtube.com/watch?v=9SgpcpZU-tw

WARNING: Your copy of avconv is outdated and unable to properly mux separate video and audio files, youtube-dl will download single file media. Update avconv to version 10-0 or newer to fix this.

Use instead:

youtube-dl  --prefer-ffmpeg  https://www.youtube.com/watch?v=9SgpcpZU-tw

.

Sunday, September 27, 2015

Embedding Subtitles in a Video File

The easiest way is with the MP4Box tool.

First install the GPAC

sudo apt-get install gpac

Then you can use the following command

MP4Box  -add  inputfile.srt  target.mp4

More about this command in this link.

Another Option to do so:

apt-get install mkvtoolnix

mkvmerge -o output.mkv video.mp4 subtitles.srt

or

mkvmerge -o output.mp4 video.mp4 subtitles.srt

In the case you need subtitle conversion:

ffmpeg -i file.srt file.vtt

or

ffmpeg -i file.vtt file.srt

More info in the ffmpeg manual ("man ffmpeg").

How to download only subtitles of videos using youtube-dl

Options:
--write-sub                      Write subtitle file
--write-auto-sub                 Write automatic subtitle file (YouTube only)
--all-subs                       Download all the available subtitles of the video
--list-subs                      List all available subtitles for the video
--sub-format FORMAT              Subtitle format, accepts formats preference, for example: "srt" or "ass/srt/best"
--sub-lang LANGS                 Languages of the subtitles to download (optional) separated by commas, use IETF language tags like 'en,pt'

So for example, to list all subs for a video:

youtube-dl --list-subs https://www.youtube.com/watch?v=Ye8mB6VsUHw

To download all subs, but not the video:

youtube-dl --all-subs --skip-download https://www.youtube.com/watch?v=Ye8mB6VsUHw

The other way, this really works but makes the file bigger:

mencoder movie.avi -sub movie.srt -o movie.hardsubs.avi -oac copy -ovc lavc -lavcopts vbitrate=1200

Source: Ubuntu Forums.


-----------///

Thursday, October 24, 2013

Converting Still Images to Video from the Command Line

ImageMagick and ffmpeg combined can be used to turn still images into video. Both ImageMagick and ffmpeg have been around for years, and are readily available as packages with most Linux distributions. The tips below allow you to convert images on the command line, which for a may appear to be time consuming, but once you know how to do it you'll find it much faster than using a GUI!

The Packages
You'll need:
ImageMagick
ffmpeg


Resizing Images from the Command Line
ImageMagick includes "mogrify" which is a fine tool for easily changing image size and resolution. Great for converting images for uploading to the web, or preparing large images for video. For example, the following code re-sizes all *.JPG images to a maximum width of 800 pixels and a maximum height of 600 pixels. This will over-write the originals, so take copies of your snaps first!

mogrify -resize 800x600 *.JPG

Morphing Images Together
This step is only needed if you want to create a smooth transition between the images.

Another fine part of ImageMagick is the ability to morph images together. In the following example we take all *.JPG files and create 10 images for each "gap" between the images. The resulting set of images are saved as XXXXX.morph.jpg, where X is the next number in the series.

convert *.JPG -delay 10 -morph 10 %05d.morph.jpg

Creating a Video from Images
ffmpeg can be used to stitch several images together into a video. There are many options, but the following example should be enough to get started. It takes all images that have filenames of XXXXX.morph.jpg, where X is numerical, and creates a video called "output.mp4". The qscale option specifies the picture quality (1 is the highest, and 32 is the lowest), and the "-r" option is used to specify the number of frames per second.

ffmpeg -r 25 -qscale 2 -i %05d.morph.jpg output.mp4

Putting It All Together
Here's a simple example of how to create a video from the command line, using the resizing and morphing techniques we have gone through above. The example first creates a "temp" directory to work in, so we don't risk destroying the original images. Note: the morphing stage may take a while, so be patient!

mkdir temp
cp *.JPG temp/.
mogrify -resize 800x800  temp/*.JPG
convert temp/*.JPG -delay 10 -morph 10 temp/%05d.jpg
ffmpeg -r 25 -qscale 2  -i temp/%05d.jpg output.mp4
# rm -R temp

The Code Used

# Create a directory and copy the original images there for manipulation:
mkdir temp
cp *.JPG temp/.

# Resize the images:
mogrify -resize 200x200  temp/*.JPG
# Create the morph images
convert temp/*.JPG -delay 10 -morph 5 temp/%05d.jpg
# Stitch them together into a video
ffmpeg -r 50 -qscale 2  -i temp/%05d.jpg output.mp4

---------------------------///
In order to make a video out of still images, like in a Power Point or Libre Office Impress, I created one image from every slide starting from a blanc one, naming them form 000.rdp.jpg to 025.rdp.jpg, then I used the command:

avconv -r .1 -qscale 2 -i %03d.rdp.jpg output.mp4

The "-r .1" gave 10 seconds lapse for every slide.


----------
Source: It For Everyone.

Friday, May 29, 2009

Converting VOB or MP4 to FLV format

FLV file converter in Linux
(link to page)
Using "mencoder" to create "flv" from "VOB" DVD video format files

Many websites nowadays are using swf and flv file formats to play videos hosted on their site or sites, it's advantage is clear in that the file itself is often in comparison to regular video format files, smaller and easier to upload so the site is able to hold and run more video content with much less bandwidth than would normally be required.

#> mencoder inputfile.VOB -of lavf -ovc lavc -lavcopts vcodec=flv:vbitrate=150 -ofps 25 -oac mp3lame -lameopts abr:br=32 -srate 22050 -vf scale=720 -o outputfile.flv

In our first test video conversion format a file named VTS_01_2.VOB was successfully converted from a 776.9Mb file to a 17mb outputfile.flv although the video and audio was brilliant the file stopped short of it's actual completion target in other words outputfile.flv was considerably shorter than it should have been, we have concluded from this that the VOB file is corrupt and the conversion terminates at that point. Having obtained another dvd with which to copy from we tried again, you should not this recording was original obtained from a small video recorder and transfered to DVD which we then migrated to a flash video (flv). Below is how we archived it, please note the above mencoder script is different to the one below which did eventually work.

#> mplayer dvd://1 -dumpstream -dumpfile myvideo.vob
#> mencoder myvideo.vob -of lavf -ovc lavc -lavcopts vcodec=flv:vbitrate=150 -ofps 25 -oac mp3lame -lameopts abr:br=32 -srate 8000 -vf scale=720 -o outputfile.flv

It is important to note the above "mplayer" command it does not play the file not visibly at least but dumps or rips 1 or more VTS_01_n.VOB files into a single "vob" extension file that can then be converted into a video file format you require. The input file has been altered to reflect the file output from mplayer and "-srate" has been altered to "8000". If you think a graphical user interface is the way to go you may be right it would take a lot of the guess work and fun out of using command line, but for now command line seems much better than "gui". Ahh problems with the above it turns out with an "-srate of 8000" you might get some rapid clicking when transfered to a website or you tube, through some investigation and some trial and error we came up with the following which you might think is much better.

#> mplayer dvd://1 -dumpstream -dumpfile myvideo.vob
#> mencoder myvideo.vob -of lavf -ovc lavc -lavcopts vcodec=flv:vbitrate=150 -ofps 25 -oac mp3lame -lameopts abr:br=32 -srate 44100 -vf scale=720 -o outputfile.flv

The "flv" format files can only support 8000, 22050 and 44100 I believe for the available -srate.
----------------------------------
Converting MP4 to FLV
ffmpeg -i myvideo.mp4 -ar 44100 myvideo.flv

Saturday, August 11, 2007

Video processing in Linux

To split video, one good tool is Avidemux.

And to compress the video the command transcode might be used.
transcode -i myinput.mpg -o myoutput.avi -y xvid4

Other useful commands are mencoder and ffmpeg
http://gentoo-wiki.com/HOWTO_Mencoder_Introduction_Guide
http://electron.mit.edu/~gsteele/ffmpeg/