Log in

View Full Version : FLV to MKV


Foofaraw
15th May 2011, 15:01
Anybody know of a program which will take an FLV and put the streams in an MKV (Much like mkvmerge could if it supported FLV's but can't because it doesn't support FLV's - and apparently won't)

sneaker_ger
15th May 2011, 15:03
You can do it in two steps:
1.) extract video, audio and timecodes using flvextract
2.) merge everything with mkvmerge

Foofaraw
15th May 2011, 17:28
You can do it in two steps:
1.) extract video, audio and timecodes using flvextract
2.) merge everything with mkvmerge

Yeah not what i asked for.

(And doesn't work, goes out of sync, plus its too much bother)

sneaker_ger
15th May 2011, 17:34
If it goes out of sync it could be because you didn't use the timecodes. If you want to do it in a single step, try ffmpeg.

Foofaraw
15th May 2011, 17:44
If it goes out of sync it could be because you didn't use the timecodes.

How do I use the timecodes? mkvmerge says its an unknown type?


If you want to do it in a single step, try ffmpeg.


Won't that reencode it?

sneaker_ger
15th May 2011, 17:47
How do I use the timecodes? mkvmerge says its an unknown type?

In mmg mark the video track and then load the timecodes at the bottom.
For cli see:
https://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge.html#mkvmerge.external_timecode_files

Won't that reencode it?

Not if you specify -vcodec copy and -acodec copy

Foofaraw
15th May 2011, 18:08
In mmg mark the video track and then load the timecodes at the bottom.
For cli see:
https://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge.html#mkvmerge.external_timecode_files


Yeah that doesn't work at all, for one thing mmg still asks me to enter a ramerate, and with the timecode file included the output now says its 18 hours long, and its not possible to seek in the mkv.




Not if you specify -vcodec copy and -acodec copy

Thanks, i'll keep it in mind.

Foofaraw
15th May 2011, 20:34
If you want to do it in a single step, try ffmpeg.

Excellent advice sneaker_ger, that worked very well. Thanks :)

Foofaraw
16th August 2011, 01:47
Don't know if you are still around sneaker_ger, but now it no longer works.

I'm getting the following annoying error from FFMpeg

[matroska @ 036A0560] Application provided invalid, non monotonically increasing
dts to muxer in stream 0: 67 >= 34
av_interleaved_write_frame(): Invalid argument


Any ideas on how to avoid that?

Command line is:

ffmpeg -i file.flv -vcodec copy -acodec copy file.mkv

(Version N-31774-g6c4e9ca)

sneaker_ger
16th August 2011, 02:02
I'm still around, but I don't know how to avoid that since I'm not an ffmpeg dev. (Maybe roozhou's builds (http://forum.doom9.org/showthread.php?t=152419) help? He has made some fixes in the past and also allows modifying fps)
Or you could try the flvextract/mkvmerge combo for those files that fail. Then you could also take a look at the timecodes and manually correct them in case of non monotonically increasing timestamps. (Though I don't know if the error is about the container or the bitstream)

If this happens only with newer ffmpeg builds but with all flv files (including those that worked in the past) uploading a sample/filing an ffmpeg bug report might be more promising.

Midzuki
16th August 2011, 04:22
@ Foofaraw: IF you're using Windows :devil: , then you could give a try to Gabest's Matroska Muxer (requires Graphstudio or Graphedit).

Foofaraw
16th August 2011, 14:50
I'm still around, but I don't know how to avoid that since I'm not an ffmpeg dev. (Maybe roozhou's builds (http://forum.doom9.org/showthread.php?t=152419) help? He has made some fixes in the past and also allows modifying fps)


Thanks I'll have a look.



Or you could try the flvextract/mkvmerge combo for those files that fail. Then you could also take a look at the timecodes and manually correct them in case of non monotonically increasing timestamps. (Though I don't know if the error is about the container or the bitstream)


Yeah, flvextract/mkvmerge does work, but then I have to do it manually, I preferred the automated way of doing it with ffmpeg


If this happens only with newer ffmpeg builds but with all flv files (including those that worked in the past) uploading a sample/filing an ffmpeg bug report might be more promising.

Well i was using an older version, then when the error started happening i tried a new version. Not sure if its their bug or me doing something wrong, but i suppose i could upload a clip.


@ Foofaraw: IF you're using Windows :devil: , then you could give a try to Gabest's Matroska Muxer (requires Graphstudio or Graphedit).

Sounds convoluted :) If i have to do it manually I already got flvextract/mkvmerge working.

sneaker_ger
16th August 2011, 22:23
If you're after automation, you can try the following batch (save as "whatever.bat" with a text editor):
@echo off

set flvextractcl=c:\program files (x86)\FLV_Extract\FLVExtractCL.exe
set mkvmerge=c:\program files (x86)\mkvtoolnix\mkvmerge.exe

if [%1]==[] goto :eof
:loop
"%flvextractcl%" -v -a -t -o %1
IF EXIST "%~dpn1.264" set video="%~dpn1.264"
IF EXIST "%~dpn1.avi" set video="%~dpn1.avi"
IF EXIST "%~dpn1.aac" set audio="%~dpn1.aac"
IF EXIST "%~dpn1.mp3" set audio="%~dpn1.mp3"
"%mkvmerge%" -o "%~dpn1.mkv" "--forced-track" "0:no" "--compression" "0:none" "--timecodes" "0:%~dpn1.txt" "-d" "0" "-A" "-S" "-T" "--no-global-tags" "--no-chapters" %video% "--forced-track" "0:no" "--compression" "0:none" "-a" "0" "-D" "-S" "-T" "--no-global-tags" "--no-chapters" %audio% "--track-order" "0:0,1:0"
del %video%
del %audio%
del "%~dpn1.txt"
set video=
set audio=
shift
if not [%1]==[] goto loop

After adjusting the paths to flvextractcl and mkvmerge you can simply drag and drop (multiple) flv files on the batch file.

TheFluff
16th August 2011, 22:38
just use (g)dsmux instead of derping around with complicated multistep procedures and temp files

Foofaraw
16th August 2011, 23:13
just use (g)dsmux instead of derping around with complicated multistep procedures and temp files

Yeah, but that requires installing codecs doesn't it? The other has the virtue of being portable.

sneaker_ger, thanks for your batchfile, I'll try it in a moment and report back :)

Foofaraw
17th August 2011, 00:44
Well, the batch file produces (of course) an mkv file - but it doesn't playback right, so I'm trying to compare the differences between a manual mkv and the batchfile mkv, hopefully i can tweek you batch file.

Thanks a lot sneaker_ger! :D

sneaker_ger
17th August 2011, 01:30
Well, the batch file produces (of course) an mkv file - but it doesn't playback right, so I'm trying to compare the differences between a manual mkv and the batchfile mkv

The batchfile copies flv to mkv timestamps. If they are fucked up, the mkv might be as well, just like ffmpeg complained about "non monotonically increasing...". If you are setting a constant fps in mkvmerge GUI this problem might be avoided. You could probably get the fps with MediaInfo CLI in there, too, but it wouldn't work on vfr files and be even more hackish than it is now.

sneaker_ger
17th August 2011, 01:47
Ok, here is a very hackish, dis-recommended and purely experimental batch for pure CFR:

@echo off

set flvextractcl=c:\program files (x86)\FLV_Extract\FLVExtractCL.exe
set mkvmerge=c:\program files (x86)\mkvtoolnix\mkvmerge.exe
set mediainfo=c:\program files\Media Info\MediaInfo_CLI_0.7.48_Windows_x64\mediainfo.exe

if [%1]==[] goto :eof
:loop
"%mediainfo%" --Inform=Video;%%FrameRate%% %1 > tmpFile
set /p fps= < tmpFile
del tmpFile
if [%fps%]==[23.976] set fps=24000/1001
if [%fps%]==[29.970] set fps=30000/1001
if [%fps%]==[59.940] set fps=60000/1001
"%flvextractcl%" -v -a -o %1
IF EXIST "%~dpn1.264" set video="%~dpn1.264"
IF EXIST "%~dpn1.avi" set video="%~dpn1.avi"
IF EXIST "%~dpn1.aac" set audio="%~dpn1.aac"
IF EXIST "%~dpn1.mp3" set audio="%~dpn1.mp3"
"%mkvmerge%" -o "%~dpn1.mkv" "--compression" "0:none" "--default-duration" "1:%fps%fps" %video% "--compression" "0:none" %audio%
del %video%
del %audio%
set video=
set audio=
shift
if not [%1]==[] goto loop

But if my first batch already failed on your file, I suspect it's broken. (Your directory and/or file name do not contain any funny characters, do they?)

Foofaraw
17th August 2011, 19:00
Broken or a hack, its the Twit live stream (free for all), and even if you only grab 20 seconds, the file says the clip is 206 hours long.

However since the framerate seems to be 30 always, just hardcoding that in you original batch file worked like a charm :)

lovelove
2nd September 2011, 01:57
However since the framerate seems to be 30 always, just hardcoding that in you original batch file worked like a charm :)

can you please :script: (your modified batch file) which worked for you, :thanks:

lovelove
2nd September 2011, 02:06
ffmpeg -i file.flv -vcodec copy -acodec copy file.mkv

works for me. Playback is fine in VLC + MPC-HC. However what I noticed is MediaInfo displays weird framerates (note the location of the decimal point):

input flv file: Frame rate: 29.970 fps
output mkv file: Frame rate: 90 000.900 fps / Original frame rate: 29.970 fps

input flv file: Frame rate: 29.970 fps
output mkv file: Frame rate: 29 970.629 fps / Original frame rate : 29.970 fps

What's your take on that?

Foofaraw
2nd September 2011, 17:19
Well turned out it doesn't always work anyway.

But I just added a

set framerate=30

at the top

and added "--default-duration" "0:%framerate%fps" to the mkvmerge line

sneaker_ger
2nd September 2011, 17:39
works for me. Playback is fine in VLC + MPC-HC. However what I noticed is MediaInfo displays weird framerates (note the location of the decimal point):

input flv file: Frame rate: 29.970 fps
output mkv file: Frame rate: 90 000.900 fps / Original frame rate: 29.970 fps

input flv file: Frame rate: 29.970 fps
output mkv file: Frame rate: 29 970.629 fps / Original frame rate : 29.970 fps

What's your take on that?

ffmpeg incorrectly sets the Matroska track element "default duration" to 1000000ns(=1ms=1000fps) or similar. This flag is purely informal, players need to honor the timecode of each frame instead for playback. (It is sometimes used for refresh rate changing, though)

lovelove
2nd September 2011, 22:15
thank you sneaker_ger. What is the difference between what MediaInfo labels "Frame rate" and "Original frame rate" ?

sneaker_ger
3rd September 2011, 11:53
"Original frame rate" is derived from the timing info stored within the H.264 bitstream, "Frame rate" from the timings dictated by the Matroska container. The former is ignored on playback.