PDA

View Full Version : FLV fixer script


KSV
06-03-2012, 10:28 AM
I have created a new script for repairing the FLV files based on the flv processing code taken from my another script AdobeHDS (http://stream-recorder.com/forum/adobe-hds-downloader-t12074.html). it can help you in following scenarios.


it can fix live streams recorded with rtmpdump which are not remuxable with ffmpeg or not properly seekable.
it can extract valid flv data leaving out junk from interrupted downloads which are no longer resumable with rtmpdump. you can resume them after fixing with this script.


You can use script with following switches:
--help displays this help
--debug show debug output
--nometa do not save metadata in repaired file
--fixwindow [param] timestamp gap between frames to consider as timeshift
--in [param] input filename of flv file to be repaired
--out [param] output filename for repaired file

Download:
https://github.com/K-S-V/Scripts

greenythebeast
06-03-2012, 03:43 PM
Wonderful, thanks!

KSV
07-18-2012, 01:07 AM
Changes:
1. rebase timestamps to zero + other improvements
2. added nometa switch to remove metadata from repaired file

removing metadata from repaired file can be often useful in case of live streams because they normally have wrong duration set in metadata.

Elgero
07-22-2012, 02:56 PM
KSV, would it be difficult to write a script or modify the flvFixer script so it can read several chunks/parts from an Flv video and then save them as a single video in one process?

I have quite a few large Flv videos (some are over 2GB) I need to cut first and then join the cut parts together, but I'm having a lot of problems finding a freeware application that can do it properly.

Some applications crash with large files, because they want to load the entire video in memory. Other applications can cut only 1 part at once, which takes a lot of time and other applications don't support Nellymoser or Speex audio, even though it's raw file editing.

I'd like to edit based on seconds or perhaps milliseconds. I know that every video needs to start on a keyframe, which means it won't always cut exactly on the specified start time, but that's not a problem.


For example:

--parts 39-249,583-937,1292-1819

cut from second 39 to 249
cut from second 583 to 937
cut from second 1292 to 1819

Save the parts as a single Flv video.

greenythebeast
07-22-2012, 09:32 PM
KSV, would it be difficult to write a script or modify the flvFixer script so it can read several chunks/parts from an Flv video and then save them as a single video in one process?

I have quite a few large Flv videos (some are over 2GB) I need to cut first and then join the cut parts together, but I'm having a lot of problems finding a freeware application that can do it properly.

Some applications crash with large files, because they want to load the entire video in memory. Other applications can cut only 1 part at once, which takes a lot of time and other applications don't support Nellymoser or Speex audio, even though it's raw file editing.

I'd like to edit based on seconds or perhaps milliseconds. I know that every video needs to start on a keyframe, which means it won't always cut exactly on the specified start time, but that's not a problem.


For example:

--parts 39-249,583-937,1292-1819

cut from second 39 to 249
cut from second 583 to 937
cut from second 1292 to 1819

Save the parts as a single Flv video.

Pretty sure you can do this with ffmpeg.

Elgero
07-22-2012, 11:19 PM
Pretty sure you can do this with ffmpeg.

Thank you. Do you know if ffmpeg can cut multiple parts with one command? The only commands I can find are -ss (start time) and -t (duration) to cut one part. This is what I'm already doing and it takes too long to do this all step by step. Or perhaps I need to write a batch script to do this.

I'm not a PHP programmer, but I'll play around with the FlvFixer script and maybe I can add it myself. I rather have the PHP script, it gives me more control and I can add little things, like "duration" metadata that some video players require to be able to seek.

Elgero
07-23-2012, 03:50 PM
I've been playing around with the FlvFixer script for a while. I know that packet 2021, 4181, 8230 and 10607 are keyframes and I have currently hardcoded them as a test.

The code below works fine, it extracts the parts from keyframe 2021 to 4181 and from keyframe 8230 to 10607. The only problem I don't know how to solve is fixing the timestamps. The timestamps of the first part are fixed correctly, but not the second part.

Is there an "easy" fix for that?

while ($filePos < $fileLen)
{
$flvTag = fread($flvIn, $tagHeaderLen);
$tagPos = 0;
$packetType = ReadByte($flvTag, $tagPos);
$packetSize = ReadInt24($flvTag, $tagPos + 1);
$packetTS = ReadInt24($flvTag, $tagPos + 4);
$packetTS = $packetTS | (ReadByte($flvTag, $tagPos + 7) << 24);
$flvTag = $flvTag . fread($flvIn, $packetSize + $prevTagSize);
$totalTagLen = $tagHeaderLen + $packetSize + $prevTagSize;
if (strlen($flvTag) != $totalTagLen)
{
DebugLog("Broken FLV tag encountered! Aborting further processing.");
break;
}

$PacketCount += 1;

if (($PacketCount >= 2021 and $PacketCount <= 4181) or ($PacketCount >= 8230 and $PacketCount <= 10607))
{

if (($baseTS === false) and (($packetType == AUDIO) or ($packetType == VIDEO)))
$baseTS = $packetTS;
if ($baseTS > 1000)
{
$packetTS -= $baseTS;
WriteFlvTimestamp($flvTag, $tagPos, $packetTS);
}

switch ($packetType)
{
case AUDIO:
if ($packetTS >= $prevAudioTS - TIMECODE_DURATION * 5)
{
$FrameInfo = ReadByte($flvTag, $tagPos + $tagHeaderLen);
$CodecID = ($FrameInfo & 0xF0) >> 4;
if ($CodecID == CODEC_ID_AAC)
{
$AAC_PacketType = ReadByte($flvTag, $tagPos + $tagHeaderLen + 1);
if ($AAC_PacketType == AAC_SEQUENCE_HEADER)

greenythebeast
07-23-2012, 06:00 PM
I've been playing around with the FlvFixer script for a while. I know that packet 2021, 4181, 8230 and 10607 are keyframes and I have currently hardcoded them as a test.

The code below works fine, it extracts the parts from keyframe 2021 to 4181 and from keyframe 8230 to 10607. The only problem I don't know how to solve is fixing the timestamps. The timestamps of the first part are fixed correctly, but not the second part.

Is there an "easy" fix for that?

while ($filePos < $fileLen)
{
$flvTag = fread($flvIn, $tagHeaderLen);
$tagPos = 0;
$packetType = ReadByte($flvTag, $tagPos);
$packetSize = ReadInt24($flvTag, $tagPos + 1);
$packetTS = ReadInt24($flvTag, $tagPos + 4);
$packetTS = $packetTS | (ReadByte($flvTag, $tagPos + 7) << 24);
$flvTag = $flvTag . fread($flvIn, $packetSize + $prevTagSize);
$totalTagLen = $tagHeaderLen + $packetSize + $prevTagSize;
if (strlen($flvTag) != $totalTagLen)
{
DebugLog("Broken FLV tag encountered! Aborting further processing.");
break;
}

$PacketCount += 1;

if (($PacketCount >= 2021 and $PacketCount <= 4181) or ($PacketCount >= 8230 and $PacketCount <= 10607))
{

if (($baseTS === false) and (($packetType == AUDIO) or ($packetType == VIDEO)))
$baseTS = $packetTS;
if ($baseTS > 1000)
{
$packetTS -= $baseTS;
WriteFlvTimestamp($flvTag, $tagPos, $packetTS);
}

switch ($packetType)
{
case AUDIO:
if ($packetTS >= $prevAudioTS - TIMECODE_DURATION * 5)
{
$FrameInfo = ReadByte($flvTag, $tagPos + $tagHeaderLen);
$CodecID = ($FrameInfo & 0xF0) >> 4;
if ($CodecID == CODEC_ID_AAC)
{
$AAC_PacketType = ReadByte($flvTag, $tagPos + $tagHeaderLen + 1);
if ($AAC_PacketType == AAC_SEQUENCE_HEADER)

Will Avidemux do what you want? http://fixounet.free.fr/avidemux/

Elgero
07-23-2012, 09:03 PM
Unfortunately not. AVIDemux is one of those tools that does not support Nellymoser audio, even though it's just raw file editing.

When I load the Flv video it first complains that no audio decoder can be found. When I try to save the video anyway it complains about unsupported audio and that saving the video has failed.

KSV
07-23-2012, 11:51 PM
IMO trying to add editing capabilities (sort of) to this script is out of scope for it's intended purpose though you are free to play around with your copy.

PayNow
07-24-2012, 05:43 AM
KSV do u have any script for fix live stream audio is not sync for video.

Elgero
07-24-2012, 08:30 AM
IMO trying to add editing capabilities (sort of) to this script is out of scope for it's intended purpose though you are free to play around with your copy.

I can modify the script to extract the parts I need, that's not the problem, but I don't know how to modify the timestamps. The timestamps of the first part are fixed correctly, but the start time of the second part does not 'connect' to the end time of the first part.

If the first part is one minute long, then the timestamp of the last video frame is 1:00.724, this is correct, but the timestamp of the first video frame of the second part starts with 5:37.141.

I don't know if there's a fairly easy fix for that or if a large part of the script needs to be rewritten, which is not worth it.

KSV
07-24-2012, 12:16 PM
I don't know if there's a fairly easy fix for that or if a large part of the script needs to be rewritten, which is not worth it.

without actually trying to make it do so i can't say for sure but it may be a bit tricky. $baseTS value should be updated with new starting timestamp for correct rebasing.

Bahman
08-07-2012, 12:49 PM
KSV, Can you please add trim option in this script?

KSV
08-08-2012, 07:42 AM
KSV, Can you please add trim option in this script?

trimming what?

Bahman
08-08-2012, 10:01 AM
Trimming Video! For example, I give it a duration or imputing a value that trim from a time to another time! like start and stop of rtmpdump!

Elgero
12-18-2012, 02:24 PM
KSV is there any way you can modify the script to fix the videos from MyFreeCams.com, Naked.com and Cams.com?

These websites deliberately put a random wrong timestamp in the video after 2/10th of a second, so most video players are not able to play the recorded videos properly. VLC can play them, but most other video players play only those first 2/10th of a second.

The only frames with the correct timestamp are the keyframes.

http://i45.tinypic.com/f3c8p5.png

svnpenn2
12-18-2012, 06:03 PM
These websites deliberately put a random wrong timestamp in the video after 2/10th of a second, so most video players are not able to play the recorded videos properly. VLC can play them, but most other video players play only those first 2/10th of a second.

Upload sample video, I may have answer.

Elgero
12-18-2012, 06:29 PM
Here's a small sample video.

<sample video deleted>

svnpenn2
12-18-2012, 07:41 PM
I could be wrong but it appears you can fix this by simply remuxing with FFmpeg

$ ffmpeg -i sample.flv -c copy a.flv
ffmpeg version N-47062-g26c531c Copyright (c) 2000-2012 the FFmpeg developers
built on Nov 25 2012 12:25:21 with gcc 4.7.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runt
ime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass -
-enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enab
le-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-libo
pus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheo
ra --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-li
bvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --ena
ble-zlib
libavutil 52. 9.100 / 52. 9.100
libavcodec 54. 77.100 / 54. 77.100
libavformat 54. 37.100 / 54. 37.100
libavdevice 54. 3.100 / 54. 3.100
libavfilter 3. 23.102 / 3. 23.102
libswscale 2. 1.102 / 2. 1.102
libswresample 0. 17.101 / 0. 17.101
libpostproc 52. 2.100 / 52. 2.100
[flv @ 000000000032dd60] DTS discontinuity in stream 0: packet 6 with DTS 129, p
acket 7 with DTS 420679
Input #0, flv, from 'sample.flv':
Duration: 00:08:38.21, start: 0.000000, bitrate: 282 kb/s
Stream #0:0: Video: flv1, yuv420p, 480x360, 1k tbr, 1k tbn, 1k tbc
Stream #0:1: Audio: nellymoser, 22050 Hz, mono, flt
Output #0, flv, to 'a.flv':
Metadata:
encoder : Lavf54.37.100
Stream #0:0: Video: flv1 ([2][0][0][0] / 0x0002), yuv420p, 480x360, q=2-31,
1k tbn, 1k tbc
Stream #0:1: Audio: nellymoser ([6][0][0][0] / 0x0006), 22050 Hz, mono
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[flv @ 0000000003f102e0] st:0 PTS: 743 DTS: 743 < 421128 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 1757 DTS: 1757 < 422167 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 2784 DTS: 2784 < 423187 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 3803 DTS: 3803 < 424206 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 4791 DTS: 4791 < 425197 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 5806 DTS: 5806 < 426210 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 6828 DTS: 6828 < 427229 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 7862 DTS: 7862 < 428260 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 8883 DTS: 8883 < 429289 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 9902 DTS: 9902 < 430284 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 10896 DTS: 10896 < 431298 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 11914 DTS: 11914 < 432317 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 12936 DTS: 12936 < 433337 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 13928 DTS: 13928 < 434333 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 14962 DTS: 14962 < 435351 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 15985 DTS: 15985 < 436374 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 17002 DTS: 17002 < 437407 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 18030 DTS: 18030 < 438434 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 19045 DTS: 19045 < 439451 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 20064 DTS: 20064 < 440469 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 21054 DTS: 21054 < 441456 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 22122 DTS: 22122 < 442511 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 23114 DTS: 23114 < 443502 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 24165 DTS: 24165 < 444540 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 25149 DTS: 25149 < 445562 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 26145 DTS: 26145 < 446549 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 27165 DTS: 27165 < 447569 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 28186 DTS: 28186 < 448591 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 29233 DTS: 29233 < 449577 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 30194 DTS: 30194 < 450632 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 31256 DTS: 31256 < 451636 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 32244 DTS: 32244 < 452656 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 33271 DTS: 33271 < 453675 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 34283 DTS: 34283 < 454689 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 35275 DTS: 35275 < 455675 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 36297 DTS: 36297 < 456700 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 37315 DTS: 37315 < 457718 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 38349 DTS: 38349 < 458714 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 39343 DTS: 39343 < 459747 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 40360 DTS: 40360 < 460767 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 41387 DTS: 41387 < 461791 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 42411 DTS: 42411 < 462846 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 43430 DTS: 43430 < 463834 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 44445 DTS: 44445 < 464823 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 45436 DTS: 45436 < 465840 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 46473 DTS: 46473 < 466878 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 47490 DTS: 47490 < 467917 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 48521 DTS: 48521 < 468965 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 49536 DTS: 49536 < 469938 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 50527 DTS: 50527 < 470928 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 51548 DTS: 51548 < 471961 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 52641 DTS: 52641 < 473061 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 53664 DTS: 53664 < 474068 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 54655 DTS: 54655 < 475061 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 55672 DTS: 55672 < 476074 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 56700 DTS: 56700 < 477101 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 57717 DTS: 57717 < 478119 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 58769 DTS: 58769 < 479113 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 59735 DTS: 59735 < 480123 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 60733 DTS: 60733 < 481119 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 61780 DTS: 61780 < 482163 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 62765 DTS: 62765 < 483180 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 63795 DTS: 63795 < 484196 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 64840 DTS: 64840 < 485245 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 65806 DTS: 65806 < 486209 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 66819 DTS: 66819 < 487225 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 67812 DTS: 67812 < 488217 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 68844 DTS: 68844 < 489246 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 69863 DTS: 69863 < 490298 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 70920 DTS: 70920 < 491294 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 71908 DTS: 71908 < 492314 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 72900 DTS: 72900 < 493305 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 73922 DTS: 73922 < 494325 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 74943 DTS: 74943 < 495345 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 75971 DTS: 75971 < 496361 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 76960 DTS: 76960 < 497350 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 77982 DTS: 77982 < 498419 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 79002 DTS: 79002 < 499380 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 79992 DTS: 79992 < 500428 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 81046 DTS: 81046 < 501456 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 82062 DTS: 82062 < 502441 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 83052 DTS: 83052 < 503464 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 84097 DTS: 84097 < 504499 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 85116 DTS: 85116 < 505518 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 86133 DTS: 86133 < 506534 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 87157 DTS: 87157 < 507555 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 88144 DTS: 88144 < 508578 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 89134 DTS: 89134 < 509539 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 90145 DTS: 90145 < 510560 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 91189 DTS: 91189 < 511605 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 92210 DTS: 92210 < 512614 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 93222 DTS: 93222 < 513626 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 94221 DTS: 94221 < 514625 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 95233 DTS: 95233 < 515637 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 96288 DTS: 96288 < 516692 invalid, clipping
[flv @ 0000000003f102e0] st:0 PTS: 97282 DTS: 97282 < 517685 invalid, clipping
frame= 1448 fps=0.0 q=-1.0 Lsize= 17887kB time=00:08:38.22 bitrate= 282.8kbits
/s
video:17305kB audio:526kB subtitle:0 global headers:0kB muxing overhead 0.313095
%

Elgero
12-18-2012, 09:10 PM
I could be wrong but it appears you can fix this by simply remuxing with FFmpeg

FFmpeg fixes the video, so it works in every video player, but it doesn't fix the timestamps. It only changes the timestamp of the keyframes, so they match the other frames.

With the sample video I posted above, I need to skip to 7 minutes before it starts to play the video. The first 7 minutes of the video is basically a still image of the first frame.

First image is before fixing, second image is after fixing.


http://i45.tinypic.com/f3c8p5.png http://i46.tinypic.com/2rp7n9s.png

KSV
12-19-2012, 07:50 AM
@Elgero

i have made some specific fixes to correct files with such odd timestamps. try it out and post your feedback.

http://pastebin.com/2CeNeArd

Elgero
12-19-2012, 10:31 AM
KSV, thank you very much. The timestamps are fixed correctly now.

TubeDigger
12-19-2012, 08:56 PM
Yet Another FLV Fixer: C file (http://speedy.sh/KvJbw/yaflvfix.c) and compiled binary (http://speedy.sh/Gh52U/yaflvfix-bin.zip). Please test and provide your feedback.

Elgero
12-19-2012, 09:19 PM
TubeDigger, that one works fine too.



The only thing I've noticed is that VLC, MPC and WMP play the fixed videos without problems, but for example BSPlayer keeps playing the first 2/10th of a second over and over again (with both FLV Fixers). It must be BSPlayer, but I wonder what the problem might be. BSPlayer has no problems with playing FLV videos that don't have these odd timestamps.

svnpenn2
12-19-2012, 09:31 PM
BSPlayer keeps playing the first 2/10th of a second over and over again

As workaround, you can try clipping first second

ffmpeg -ss 1 a.flv -c copy b.flv

Elgero
12-19-2012, 09:45 PM
As workaround, you can try clipping first second

ffmpeg -ss 1 a.flv -c copy b.flv

Thank you very much. BSPlayer can now play them as well.

reumb
12-20-2012, 07:48 AM
Yet Another FLV Fixer: C file (http://speedy.sh/KvJbw/yaflvfix.c) and compiled binary (http://speedy.sh/Gh52U/yaflvfix-bin.zip). Please test and provide your feedback.


this is a best windows noob tool seen here (in my opinion)

just draw/drop your flv f4v to yaflvfix.exe

works allmost on every file ------- great1!

TubeDigger
12-20-2012, 08:19 AM
works allmost on every file

For further improvements please upload that problematic videos and post links to them here or via PM.

KSV
12-21-2012, 07:28 AM
i have pushed the update to repo (https://github.com/K-S-V/Scripts) with a bit of more fine tuning.

@TubeDigger

Thanx for your contributions.

TubeDigger
01-01-2013, 05:53 PM
Slightly improved Yet Another FLV Fixer: C file (http://speedy.sh/uT8YZ/yaflvfix.c) and compiled binary (http://speedy.sh/B2psz/yaflvfix-bin.zip).

reumb
01-02-2013, 09:03 AM
Thanks for the update TubeDigger

Best flvfixer ever made for windows noobs/pros (in my opinion)

just drag/drop your flv f4v on yaflvfix.exe

grobatob
01-19-2013, 11:44 PM
Hi, I have a problem that I suppose most people don't have because I have "broken" the file I need fixed myself.

The problem is I have a series of flv files dumped with rtmpdump that I want to merge, which have identical video/audio streams with overlap so they can't simply be connected one after the other. (That is for example file1 goes from 0-1hr file2 goes 45mins to 2 hours, etc) I have linux so I am unable to try most of the merging tools I found with a google search, but avconv and ffdshow both seem to be unable to do it.

The files all have identical video/audio streams and since they overlap I went and found a spot they are the same in a hex editor and simply used dd to trim them then cat to join them together. Once concatenated together the playback is perfect and seamless in the version of mplayer2 I have installed (2.1~git20120828.28228-1) from start to end, however it doesn't show total play time and the OSD time jumps backwards when it hits the join point in the files with the message:

Decreasing video pts: 4.743000 < 50.986000

Playing the same file in VLC causes a large skip, presumably VLC is skipping ahead to the next point in the file where the pts is greater than the last one it played.

I was hoping these concatenated files could simply be run through ffmpeg with '-codec copy' but when ffmpeg (most recent compiled from their git) gets to the join it gives a series of errors like this:

[flv @ 0x204cba0] st:0 PTS: 41471 DTS: 41471 < 50991 invalid, clipping

The resulting file has the wrong total time, and when it gets to the join the video skips ahead a fair bit (in mplayer2) then the video ends early while the audio continues to play without any skip and goes its full length. VLC behaves the same as it did before running the file through ffmpeg

So I have been searching around trying to figure out how to fix the timestamps. Presumably they just need to be resequenced so they line up as they should. I looked at the ffmpeg source code where the 'invalid, clipping' message is to see if I could edit it to adjust the timestamps instead of clipping but it is quite a bit over my head.

So that is what brings me here. I have tried both the php script and compiled the latest yaflvfix.c which works fine on linux. The php script gives weird artefacting at the join which other attempts to fix haven't and the same large skip/too short video playback. The yaflvfix program gives seemingly identical results to ffmpeg, but the source code is a lot easier to understand so I think it is the closest I have gotten to fixing this problem.

Now I am trying to work from the yaflvfix source to see if I can edit it to meet my needs, but I was hoping someone could help me as I am completely clueless about all of this. Maybe someone could do it very easily, whereas for me it is very difficult. I know nothing about video streams or editing them at all.

Here is a 12000682 byte sample clip showing my problem. The first 6000000 bytes are from the first video file and contain the unmodified header, the last 6000682 bytes are from the second file which I need to join and of course are taken from the middle of some video/audio data which is identical in both files. This isn't exactly where I need to merge them but is the shortest clip I could make where they overlap. The actual files are much larger.

http://www.mediafire.com/?y2h9o6dml11kaz1

Any assistance people could give with fixing this would be greatly appreciated. If the yaflvfix.c program can be modified to handle this type of file I think that seems most promising right now. As I said this file plays flawlessly in a recent mplayer2 build so there is no video or audio corruption at all, it appears to be entirely a problem with timestamps and how various software deals with the timestamps.

Thank you for reading, and thank you for any help you can give.

KSV
01-20-2013, 10:13 AM
@grobatob

Try this one:
http://pastebin.com/NHCE3c3f

grobatob
01-20-2013, 06:32 PM
@grobatob

Try this one:
http://pastebin.com/NHCE3c3f

Wow, thank you so much this seems to work exactly as I had needed! I only tried it on that sample file and a larger one with a single "seam" for now but it does exactly what I had hoped for! I can even now run it through ffmpeg without error to losslessly remux as mp4. Great!

I will try it on a much larger file soon with several "seams" and let you know how it goes, but I anticipate it would work fine. At any rate I could probably just join one file at a time then run the fix after each join if needed.

Will these changes eventually be merged into the main script you host on github, or is it something too custom and alters the behaviour in a way that it no longer works for other files it was originally intended for?

Thanks again. You fixed it so fast when I had been struggling with it for a long time. Your work is very much appreciated.

KSV
01-21-2013, 04:22 AM
i have pushed the update to the repo. hopefully it wouldn't break any previous functionality. before reporting any problem with any of my scripts make sure you are using the latest version of the script from repo.

KSV
03-02-2013, 07:38 AM
Changes:

1. improved performance of FlvFixer

Batch file for drag and drop functionality:
@echo off
title KSV FlvFixer Script

rem == Set input output filenames ==
set IN_FILE="%~dpnx1"
set OUT_FILE="%~dpn1_fixed.flv"

rem == Change current directory ==
cd "%~dp0"
cd %~d0

php FlvFixer.php --in %IN_FILE% --out %OUT_FILE% --debug --nometa 2> Debug.txt

Mary Chris Smith
03-02-2013, 01:15 PM
Hey KSV,
I want to fix a FLV file which rtmpdump (live stream) generated.
I get the following error:
FlvFixer.php --debug --nometa --in foo.flv --out bar.flv
KSV FLV Fixer

Type CurrentTS PreviousTS Size Position
VIDEO 0 -1 15641 13
Fixing video timestamp
VIDEO 0 0 7796
VIDEO 8 0 7796 15669
Fixing video timestamp
VIDEO 0 8 9197
VIDEO 16 8 9197 23480
Fixing video timestamp
VIDEO 0 16 7792
VIDEO 24 16 7792 32692
Fixing video timestamp
VIDEO 0 24 8003
VIDEO 32 24 8003 40499
Fixing video timestamp
VIDEO 0 32 7762
VIDEO 40 32 7762 48517
Fixing video timestamp
VIDEO 0 40 9007
VIDEO 48 40 9007 56294
Fixing video timestamp
VIDEO 0 48 7926
VIDEO 56 48 7926 65316
Fixing video timestamp
VIDEO 0 56 9196
VIDEO 64 56 9196 73257
Fixing video timestamp
VIDEO 0 64 421
VIDEO 72 64 421 82468
Fixing video timestamp
VIDEO 0 72 7848
VIDEO 80 72 7848 82904
Fixing video timestamp
VIDEO 0 80 7474
VIDEO 88 80 7474 90767
Fixing video timestamp
VIDEO 0 88 8066
VIDEO 96 88 8066 98256
Fixing video timestamp
VIDEO 0 96 7232
VIDEO 104 96 7232 106337
Fixing video timestamp
VIDEO 0 104 8107
VIDEO 112 104 8107 113584
Fixing video timestamp
VIDEO 0 112 7724
VIDEO 120 112 7724 121706
Broken FLV tag encountered! Aborting further processing.
Processed input file in 0.06 seconds
Finished

KSV
03-02-2013, 11:52 PM
@Mary Chris Smith

Well script isn't lying. rtmpdump actually saved the corrupt file. it's a known bug in official version which i fixed ages ago. you need to use my version of rtmpdump (http://www.mediafire.com/file/a2u20kc21bi4idc/rtmpdump-2.4.zip) to save the proper file in such cases. if you wanna recover the already saved files you have to fix them first by hand.

Mary Chris Smith
03-03-2013, 11:29 AM
@Mary Chris Smith

Well script isn't lying. rtmpdump actually saved the corrupt file. it's a known bug in official version which i fixed ages ago. you need to use my version of rtmpdump (http://www.mediafire.com/file/a2u20kc21bi4idc/rtmpdump-2.4.zip) to save the proper file in such cases. if you wanna recover the already saved files you have to fix them first by hand.

Ok, I'll try your version, thanks!

Mary Chris Smith
03-03-2013, 11:42 AM
Ok, I'll try your version, thanks!

Mh. I tested your version and now I can't download it at all.
It always stops on: ERROR: Closing connection: NetStream.Play.StreamNotFound
With the officlial rtmpdump version I can download the stream. Sometimes the file is fine, sometimes it is corrupt, but it works.

mehphishtopheles
07-23-2013, 04:38 AM
@Mary Chris Smith

Well script isn't lying. rtmpdump actually saved the corrupt file. it's a known bug in official version which i fixed ages ago. you need to use my version of rtmpdump (http://www.mediafire.com/file/a2u20kc21bi4idc/rtmpdump-2.4.zip) to save the proper file in such cases. if you wanna recover the already saved files you have to fix them first by hand.

KSV, can you describe the by-hand fix you mentioned above?

I have some live files I ripped 3 weeks ago with the outdated (2.4 original) version of rtmpdump. They do appear to be corrupt, as they crash every tool i throw at them.

I have no way to re-obtain another dump since it was a live concert webcast. An example of what I am working with for source material:
https://mega.co.nz/#!lxlSVKJD!XpjbrTYY9v_NtYt0N33tkGy68gQlkDYKoCkkHGe HSFE

mehphishtopheles
07-23-2013, 05:07 AM
This is the output I get when running your php script:

# php flvfixer.php --nometa --in 1a.flv --out fixed.flv --debug

KSV FLV Fixer

Type CurrentTS PreviousTS Size Position
Writing AVC sequence header
VIDEO 0 -1 66 13
Writing AAC sequence header
AUDIO 0 -1 4 94
VIDEO 0 0 52212 113
Fixing video timestamp
VIDEO 0 0 89
VIDEO 8 0 89 52340
Fixing video timestamp
VIDEO 0 8 91
VIDEO 16 8 91 52444
Fixing video timestamp
VIDEO 0 16 92
VIDEO 24 16 92 52550
Fixing video timestamp
VIDEO 0 24 96
VIDEO 32 24 96 52657
Fixing video timestamp
VIDEO 0 32 98
VIDEO 40 32 98 52768
Fixing video timestamp
VIDEO 0 40 125
VIDEO 48 40 125 52881
Fixing video timestamp
VIDEO 0 48 247
VIDEO 56 48 247 53021
Fixing video timestamp
VIDEO 0 56 390
VIDEO 64 56 390 53283
Fixing video timestamp
VIDEO 0 64 765
VIDEO 72 64 765 53688
Fixing video timestamp
VIDEO 0 72 873
VIDEO 80 72 873 54468
Fixing video timestamp
VIDEO 0 80 2197
VIDEO 88 80 2197 55356
Fixing video timestamp
VIDEO 0 88 2313
VIDEO 96 88 2313 57568
Fixing video timestamp
VIDEO 0 96 3001
VIDEO 104 96 3001 59896
Fixing video timestamp
VIDEO 0 104 2810
VIDEO 112 104 2810 62912
Fixing video timestamp
VIDEO 0 112 3379
VIDEO 120 112 3379 65737
Fixing video timestamp
VIDEO 0 120 4011
VIDEO 128 120 4011 69131
Fixing video timestamp
VIDEO 0 128 4121
VIDEO 136 128 4121 73157
Fixing video timestamp
VIDEO 0 136 4063
VIDEO 144 136 4063 77293
Fixing video timestamp
VIDEO 0 144 4930
VIDEO 152 144 4930 81371
Fixing video timestamp
VIDEO 0 152 5275
VIDEO 160 152 5275 86316
Fixing video timestamp
VIDEO 0 160 5443
VIDEO 168 160 5443 91606
Fixing video timestamp
VIDEO 0 168 5492
VIDEO 176 168 5492 97064
Fixing video timestamp
VIDEO 0 176 5662
VIDEO 184 176 5662 102571
Fixing video timestamp
VIDEO 0 184 5826
VIDEO 192 184 5826 108248
Fixing video timestamp
VIDEO 0 192 5861
VIDEO 200 192 5861 114089
Fixing video timestamp
VIDEO 0 200 5996
VIDEO 208 200 5996 119965
Fixing video timestamp
VIDEO 0 208 5731
VIDEO 216 208 5731 125976
Fixing video timestamp
VIDEO 0 216 6348
VIDEO 224 216 6348 131722
Fixing video timestamp
VIDEO 0 224 5897
VIDEO 232 224 5897 138085
Fixing video timestamp
VIDEO 0 232 6402
VIDEO 240 232 6402 143997
Timestamp gap detected: PacketTS=1870706200 LastTS=240 Timeshift=1870705960
Timestamp gap detected: PacketTS=26934631 LastTS=240 Timeshift=26934391
Timestamp gap detected: PacketTS=227358670 LastTS=240 Timeshift=227358430
Broken FLV tag encountered! Aborting further processing.
Processed input file in 2.57 seconds
Finished

KSV
07-23-2013, 08:24 AM
i am not gonna download 2 GB file just to explain the method. only upload first 10 MB of file.

mehphishtopheles
07-23-2013, 08:53 AM
i am not gonna download 2 GB file just to explain the method. only upload first 10 MB of file.

Of course, my apologies.

Is there a windows shell command I can use to save just the first 10MB of the file into a new one? I'm not exactly sure how to do that.

Thank you for your patience and kindness. I am desperately in need of your help, as I have a number of files that have this exact problem. I know how to avoid it in the future now using your custom compiled version of rtmpdump, but would like to be able to get useable footage out of some of the stuff I already ripped with the old version.

KSV
07-23-2013, 09:24 AM
save the following script as trim.php and run the command line

php trim.php "input video.flv"

<?php
$handle = fopen($argv[1], 'rb');
$data = fread($handle, 10 * 1024 * 1024);
file_put_contents("$argv[1]" . ".trim", $data);
?>

mehphishtopheles
07-23-2013, 09:48 AM
Thank you, KSV.

Here is the trimmed file:

https://mega.co.nz/#!l5V1WSZZ!RkortCiEFJ9aNnuZk6YDTlAVDJsyu28NL7hYsdQ Z2A8

KSV
07-23-2013, 12:31 PM
i have analyzed it. it doesn't seems like the original capture from rtmpdump. have you ran any other tools on this file? it seems messed up at many places. provide original unaltered rtmpdump trim.

ZeChico
04-30-2014, 08:56 AM
Hi!

I'm sorry to necro this thread, but I have been using this script for a long time to repair my MFC recordings. I always notice that the videos, specially the long ones had a slow motion after a while and the audio was not synchronized.

Only now that I paid attention to it. It seems the problem was in the "FLV fixer.php" itself, the default value of FRAMEFIX_STEP of 40 was too high.

After many tests I found that the value of 22 is ideal so I changed the line on the script to:
define('FRAMEFIX_STEP', 22);

With this the sound seems fine and so is the motion.

To further repair the files I also created a small bat script (ex: FLVfixer.bat) to repair any FLV in the same folder, it calls the "FLV fixer.php" and uses the "yamdi.exe" (search the net for it) to recreate the metadata, so it needs this 2 files in the same folder. It first repairs the flv into a temporary file and then the yamdi uses the temporary file to rewrite the original one.

This is the DOS BAT script if anyone wants this easier way of repairing many MFC FLV files in the same folder:
@ECHO OFF
FOR %%A IN (*.flv) DO (
IF NOT "%%A"=="temp.flv" CALL :FIX "%%A"
)
GOTO END

:FIX
ECHO -------------------------------------------------------
ECHO - Fixing %1...
php "FLV fixer.php" --nometa --fixwindow 1000 --in %1 --out "temp.flv"
ECHO Injecting MetaData with yamdi...
yamdi.exe -i "temp.flv" -k -o %1
ECHO Done!
ECHO:
GOTO :eof

:END
ECHO -------------------------------------------------------
ECHO Deleting temporary file...
DEL /Q "temp.flv"
ECHO:
ECHO All done!
ECHO ON
@PAUSE

If instead of rewrite the file you instead want it to save into other folder (ex: into folder "OK") you need to edit the following line:
yamdi.exe -i "temp.flv" -k -o .\OK\\%1

To repair old flv repaired by "FLV fixer.php" that got this slow motion problem, you need to edit the "FLV fixer.php" and after editing the line:
define('FRAMEFIX_STEP', 22);

You also need to change the line:
if ($timeShift > $fixWindow)
to
if ($timeShift == 40)

Thank you KSV for this script.

Regards!

CristianoA
09-22-2016, 08:40 AM
FLVFixer for Windows anyone ?