Good morning all
For purposes of programming, I need to re-encode a short video with ffmpeg and join it with a longer video but unfortunately the larger video is variable frame rate. Consequently problems arise joining vfr with cfr. I have copied ALL x264 parameter and used vsync 0,1 and 2. Ffmpeg seems to always encode constant frame rate.
I would rather not encode both for quality loss reasons. Does anyone know a method to use FFMPEG to create a variable frame rate video?
Thank you
+ Reply to Thread
Results 1 to 16 of 16
-
-
Try
Code:ffmpeg -f concat -safe 0 -i mylist.txt -c copy out.mp4
Perhaps you can collect some ideas from here:
https://trac.ffmpeg.org/wiki/ConcatenateLast edited by Sharc; 4th Mar 2020 at 04:06.
-
As a side note: do not rely on mediainfo when you add a cfr video before a vfr video to check whether the content is vfr or not since mediainfo will only check a few timecodes at the beginning on the file which will report cfr if your cfr video at the front is long enough.
If everything fails your could always:
1. extract the time codes or the vfr part
2. extend the time codes to accommodate the cfr part
3. append the files without the time codes and add the new time codes to the output.
Cu Selur
Ps.: You could probably also first append the files with mkvmerge and then repackage the file to your target container using ffmpeg.users currently on my ignore list: deadrats, Stears555, marcorocchini -
I appreciate all the responses but it seems I need to explain a little better. I am writing a program in Visual Basic that cuts precisely on 'I' frames and if the cut is between 'I' Frames, it encodes the necessary parts to be joined to the following Key frame segment.
IBBBPBBBPBBBP Original
IBBBPBBBP Cut portion
BBBPBBBPBBBPIBBBPBBBPBBBP Original
IBBPBBBPBBBP IBBBPBBBPBBBP Encoded/Copied portions
I am using FFMPEG because It can be called behind the scenes with the necessary string to accomplish everything. With a constant Frame rate everything works flawlessly and is frame accurate. Merging is no problem.
Variable frame rate is another problem. Copied portions are variable frame rate and Encoded portions are Constant frame rate. I tried -Vsync 2 and some of the encoded sections do indeed end up variable frame rate but others are constant. The problem with joining:
Constant + variable = Constant + section that jumps and runs in fraction of what it should. (Very Fast)
Variable + Constant = Variable + freeze frame for far longer than the 2 sections together.
I have been able to copy the format, codec wise and the only difference in the MediaInfo output is the Frame rate.
Is it just impossible to encode a variable frame rate with FFMpeg? -
Try to work with the video track only, without audio and subtitles.
This should give you a cfr in both the copied and encoded parts. -
I Thought I would list my apparent fix here so anyone else might have the information. Apparently it is impossible to encode an MP4 with variable frame rate but one may be created by merging 2 constant rate videos. The is further problematic if someone tries to cut and merge the merged video, some segments may be constant frame rate and some variable.
Example:
I merged a contant frame rate, 30 minute video with another Constant frame rate 30 minute video. Everything played well but the result was listed as variable frame rate. Checking the entire video for PKT_PTS Times and there was a slight deviation at the merge point. (30 FPS was .0333 duration but at the merge point it jumped for 1 frame by .0333111. and then returned to .0333)
If I cut the first 15 minutes it was constant frame rate. If I cut from 20 minutes to 40 minutes (over the deviation) the result was listed as variable.
Since there is no way, apparently to make the constant video variable, so the only option left for cutting and merging without play back problems was to change the Variable frame rate video
PTS times without encoding. Luckily I found a buried post that seems to work.
In this example, I'm using MP4 containers. First, extract the stream:
ffmpeg -i source.mp4 -map 0:v -vcodec copy -bsf:v h264_mp4toannexb source-video.h264
Next, take that stream and re-mux it, but with a specific frame rate and generated timestamps.
ffmpeg -fflags +genpts -r 30 -i source-video.h264 -vcodec copy output.mp4
?? I do not know what the range that would be noticeable but I think this would be well below what would be detectable that unless there were many such areas.
If I am correct, I can alter my program to copy from I frames and encode only the segment between I Frames if necessary and insure all are mergeable.
Any information that may help would be appreciated. -
Apparently it is impossible to encode an MP4 with variable frame rate but one may be created by merging 2 constant rate videos.
Code:+genpts -r 30 -i
-> So using this will only avoid creating vfr when appending cfr parts (which both have 30fps).
The is further problematic if someone tries to cut and merge the merged video, some segments may be constant frame rate and some variable.
Since you now mentioned that you use mp4:
Last I checked '-vsync vfr' was default for mkv but not for mp4 and I don't think that changed.
So just to make sure: You are using '-vsync vfr' as an output option when messing with vfr and trying to keep the time codes during cutting and reencoding etc. right?
Cu Selurusers currently on my ignore list: deadrats, Stears555, marcorocchini -
Thank you for the information. It helps clarify what I had but didnt quite understand.
[QUOTE]So just to make sure: You are using '-vsync vfr' as an output option when messing with vfr and trying[ to keep the time codes during cutting and reencoding etc. right?/QUOTE]
I am trying to prevent having to reencode and it seemed that during my testing, vsync 2 or vsync cfr worked about like just copying, sometime yes sometimes no. It only seemed to work if I was encoding. So I could encode either ENCODE Cfr to cfr with vsync or encode vfr to cfr most any way.
The method above at least, doesn't seem to encode but does take quite a while to change a 2 hr movie.
Am I correct in assuming changing pts will result in slight sync change? -
Am I correct in assuming changing pts will result in slight sync change?
When you cut and/or reencode a part of vfr clip and save it as mp4 you either need to set the output frame rate this way ffmpeg will make sure it creates cfr output or you need to specify '-vsync vfr' otherwise the time codes might be messed up.
If you append cfr and vfr content to mp4 you should also add '-vsync vfr'.
Combining raw H.264 content with vfr content will likely cause problems.
"+genpts -r 30" will only adjust the time stamps and not do any reencoding, but you should not apply this on vfr content since then your output will get async.
Cu Selurusers currently on my ignore list: deadrats, Stears555, marcorocchini -
'-fflags +genpts' also works on raw content which has no time stamps, '-r XX -i ...' otherwise only overwrites existing time stamps.
(iirc without genpts you will also have problems if your source is missing time stamps like it sometimes happens with stream captures)users currently on my ignore list: deadrats, Stears555, marcorocchini -
Similar Threads
-
FFmpeg is changing the frame rate to Variable when copying video?
By kkiller23 in forum Newbie / General discussionsReplies: 5Last Post: 1st Dec 2019, 13:14 -
Making a Variable Frame Rate video
By Mr. Anderssarian in forum Video ConversionReplies: 3Last Post: 1st Jan 2019, 17:17 -
transcode Variable Frame Rate (VFR) AVC video to Constant Frame rate (CFR)
By hydra3333 in forum Video ConversionReplies: 2Last Post: 4th Mar 2018, 05:01 -
Variable frame rate into constant frame rate without re-encoding
By DunnoNo in forum Newbie / General discussionsReplies: 8Last Post: 21st Dec 2017, 11:05 -
Avidemux Variable Frame Rate
By ThatFox in forum Newbie / General discussionsReplies: 8Last Post: 23rd Jun 2015, 04:01