Audio/video stream recording forums

Attention Visitor:
You may have to register or log in before you can post:
  • Click the register link to sign up.
  • Registered members please fill in the form below and click the "Log in" button.
To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Audio/video stream recording forums > Streaming media recording forum > rtmpdump
Register FAQ Members List Calendar Mark Forums Read

Reply Post New Thread
 
Thread Tools Display Modes
  #1  
Old 06-03-2012, 11:28 AM
KSV KSV is offline
Senior Member
 
Join Date: Apr 2011
Posts: 853
KSV is on a distinguished road
Cool

FLV fixer script


I have created a new script for repairing the FLV files based on the flv processing code taken from my another script AdobeHDS. it can help you in following scenarios.
  1. it can fix live streams recorded with rtmpdump which are not remuxable with ffmpeg or not properly seekable.
  2. 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:
Code:
 --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:
Code:
https://github.com/K-S-V/Scripts

Last edited by KSV : 03-02-2013 at 08:39 AM.
Reply With Quote
  #2  
Old 06-03-2012, 04:43 PM
greenythebeast greenythebeast is offline
Senior Member
 
Join Date: Dec 2011
Posts: 105
greenythebeast is on a distinguished road
Default

Re: FLV fixer script


Wonderful, thanks!
Reply With Quote
  #3  
Old 07-18-2012, 02:07 AM
KSV KSV is offline
Senior Member
 
Join Date: Apr 2011
Posts: 853
KSV is on a distinguished road
Cool

Re: FLV fixer script


Changes:
Code:
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.
Reply With Quote
  #4  
Old 07-22-2012, 03:56 PM
Elgero Elgero is offline
Senior Member
 
Join Date: Feb 2012
Posts: 177
Elgero is on a distinguished road
Default

Re: FLV fixer script


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.
Reply With Quote
  #5  
Old 07-22-2012, 10:32 PM
greenythebeast greenythebeast is offline
Senior Member
 
Join Date: Dec 2011
Posts: 105
greenythebeast is on a distinguished road
Default

Re: FLV fixer script


Quote:
Originally Posted by Elgero View Post
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.
Reply With Quote
  #6  
Old 07-23-2012, 12:19 AM
Elgero Elgero is offline
Senior Member
 
Join Date: Feb 2012
Posts: 177
Elgero is on a distinguished road
Default

Re: FLV fixer script


Quote:
Originally Posted by greenythebeast View Post
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.
Reply With Quote
  #7  
Old 07-23-2012, 04:50 PM
Elgero Elgero is offline
Senior Member
 
Join Date: Feb 2012
Posts: 177
Elgero is on a distinguished road
Default

Re: FLV fixer script


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?

Code:
  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)
Reply With Quote
  #8  
Old 07-23-2012, 07:00 PM
greenythebeast greenythebeast is offline
Senior Member
 
Join Date: Dec 2011
Posts: 105
greenythebeast is on a distinguished road
Default

Re: FLV fixer script


Quote:
Originally Posted by Elgero View Post
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?

Code:
  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/
Reply With Quote
  #9  
Old 07-23-2012, 10:03 PM
Elgero Elgero is offline
Senior Member
 
Join Date: Feb 2012
Posts: 177
Elgero is on a distinguished road
Default

Re: FLV fixer script


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.
Reply With Quote
  #10  
Old 07-24-2012, 12:51 AM
KSV KSV is offline
Senior Member
 
Join Date: Apr 2011
Posts: 853
KSV is on a distinguished road
Cool

Re: FLV fixer script


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.
Reply With Quote
Reply Post New Thread
Tags:



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 06:55 PM.


Powered by All-streaming-media.com; 2006-2011
vB forum hacked with Zoints add-ons