ffmpeg Will stream be copied if same codec is requested? - ffmpeg

I loop over some files and convert them with ffmpeg. I provide -vcodec h264. When the input video already is encoded with that codec: will the video stream be copied? How to make sure it's not reencoded in that case? Is it what -sameq was used previously?

You need to use -c:v copy if you want the raw H.264 stream to be passed on without re-encoding:
ffmpeg -i myh264file.mp4 -c:v copy -c:a copy myh264output.mp4
-c:a copy will also copy the audio
-c copy will copy both audio and video as in:
ffmpeg -i myh264file.mp4 -c copy myh264output.mp4
Detecting H.264 streams is not straight forward. You will need to code this.
For the -sameq settings please refer to this statement.
I would recommend upgrading to a recent version of ffmpeg if it is not already done as -vcodec is not used anymore, now it is -c:v.
The documentation on ffmpeg could help you.

Related

Change video file container to mp4

I have learned through Google, that to change video containers without losing quality I can run the following command:
ffmpeg -i videofile.mkv -codec copy videofile.mp4
This has worked great for a number of video files I have. However, I am having an issue with three of them (1 mkv file and 2 avi files). When I run that command against them, the video is there, but there is no sound. There is sound in the original video file.
Any ideas how to put the video in a new container while retaining the audio track?
Thanks. Brian
The .mp4 container is not compatible with the audio codecs of the problem files. This should be evident from the logs. So the audio channels of the problem files have to be transcoded to something allowed in .mp4, eg. aac:
ffmpeg -i videofile.mkv -c:v copy -c:a aac videofile.mp4
Maybe the container mp4 it doesn’t compatible whith codec of your input video mkv and avi
By the way, you can try
ffmpeg -i videofile.mkv -c:v copy -c:a copy videofile.mp4
Check this documentation.
https://en.m.wikipedia.org/wiki/Comparison_of_video_codecs
https://trac.ffmpeg.org/wiki/Map

ffmpeg command to merge file with good quality

I am looking for a better command that can merge both audio & video files into one with a better quality.
I found this command from Muaz Khan's WebRTC APIs.
ffmpeg -i {$audioFile} -i {$videoFile} -map 0:0 -map 1:0 {$mergedFileName}
Later on server i had to add "-strict -2" with this command as on server it says that above command is experimental if I still want to use it you should add "-strict -2" with it.
It is working well but my video file (.webm) with size 2.2MB and audio file (.wav) with size 1.5MB was merged into a new file (.webm) with size 422.5KB. This new video file is having lag.
Also I want the meta information for duration of video is already written on the resulting video file.
Is there any command which can give the merged file without lagging and both video and audio of the new file are of good quality ?
Use
ffmpeg -i {$audioFile} -i {$videoFile} -map 0:0 -c:a libopus -map 1:0 -c:v copy {$mergedFileName}
This will encode only the audio, leaving the video intact. Use libvorbis if libopus isn't present in your FFmpeg.

How add scale in my ffmpeg command

i want convert video from any format to mp4. so i am using command:
ffmpeg -i ttt.mp4 -vcodec copy -acodec copy test.mp4
this is working perftectly but now i also add scale in this -s 320:240.
There also many other command for convert LIKE :
ffmpeg -i inputfile.avi -s 320x240 outputfile.avi
but after convert by this command video not play in html5 player
BUT this is not working so tell me in my command how i add scale;
So please provide me solution for this .
Thanks in advance.
You have several problems:
In your command, you have -vcodec copy you cannot scale video without reencoding.
In the command you randomly found on the Internet, they are using AVI, which is not HTML5-compatible.
What you should do is:
ffmpeg -i INPUT -s 320x240 -acodec copy OUT.mp4
Adding to Timothy_G:
Video copy will ignore the video filter chain of ffmpeg, so no scaling is available (man ffmpeg is a great source of information that you will not find on Google). Notice that once you start decoding-filtering-encoding (i.e., no copy) the process will be much slower (x100 time slower or even more). The libx264 is recommended if you want compatibility with all browsers.
$ ffmpeg -i INPUT -s 320x240 -threads 4 -c:a copy -c:v libx264 OUT.mp4
vp9 will provide nearly 50% extra bandwidth saving, but only for supported browsers (Firefox/Chrome), and the encoding will much slower compared to libx264 (that itself is much slower that v:c copy):
$ ffmpeg -i INPUT -s 320x240 -c:a copy -c:v vp9 OUT.webm
Notice that there is a set of formats (containers) accepted by browsers (most admit mp4, some also webm, ...) and for each format there is a set of audio/video codecs accepted. For example you can use mp3 or aac with an mp4 file (container), but not with webm files.
http://en.wikipedia.org/wiki/HTML5_video#Supported_video_formats

How to wrap H264 into a mp4 container?

I have a program generating a bunch of raw H264 frames and would like to place that into a mp4 container for streaming.
Anyone know how to do that?
I was thinking I'd use ffmpeg however, this needs to be used commercially and it seems like ffmpeg can only do this through it's x264 library... which uses a GPL license.
Thank you!
If you're looking for the FFMPEG command line to do that, then try the following:
ffmpeg -i "source.h264" -c:v copy -f mp4 "myOutputFile.mp4"
If you have a separate audio file you can add it too:
ffmpeg -i "source.h264" -i "myAudio" -c:v copy -c:a copy -f mp4 "myOutputFile.mp4"
If your audio needs to be encoded as well (for instance codec AAC-LC, bitrate 256kbps):
ffmpeg -i "source.h264" -i "myAudio" -c:v copy -c:a aac -b:a 256k -strict -2 -f mp4 "myOutputFile.mp4"
libmp4v2 is under the MPL and can be used as part of a larger work commercially. It is much lighter than libavformat also.

Converted mp4 h264 baseline format loads long time

I have converted my video to mp4 x264 baseline format and it works fine with all pc/mobile phones , the problem is it takes long time to load the video while googling came to know that ffmpeg converts and sets the index file at the eof the video so it loads to the end to read and then plays the video, So any advices would be appreciatable to cut short the loading time.
Note:tryied out QT index swapper2 but dint give much difference , please advice .
this is the cmd i used to convert -
ffmpeg -i … -c:v libx264 -profile:v baseline -level 1 …
Thanks for your time .
You have several options to relocate the moov atom so the video can begin playback before it is completely downloaded by the client.
-movflags faststart
The easiest is the option -movflags faststart when re-encoding:
ffmpeg -i input -c:v libx264 -profile:v baseline -movflags faststart output.mp4
If you already encoded your .mp4 file, but simply want to move the atom:
ffmpeg -i input.mp4 -codec copy -movflags faststart output.mp4
You may need to get a more recent ffmpeg version to use this option. See the FFmpeg download page for links to ffmpeg builds for Linux, OS X, and Windows, or you can follow a step-by-step guide to compile ffmpeg.
qt-faststart
Alternatively you can use the qt-faststart tool that comes with the ffmpeg source:
cd ~/ffmpeg/tools
make qt-faststart
./qt-faststart input.mp4 output.mp4
MP4Box
Or you could use MP4Box (usually provided by the gpac package depending on your distro):
MP4Box -add input.mp4 output.mp4
Also See
FFmpeg and x264 Encoding Guide
FFmpeg and AAC Audio Encoding Guide

Resources