Problem with joining files

Everything else that doesn't fall into one of the other PB categories.
urland
User
User
Posts: 13
Joined: Tue May 13, 2003 6:53 pm

Problem with joining files

Post by urland »

I did a search of the forums and could not come up with anything on this subject. I'm trying to join multiple media files into one file and then play it back (files of the same type). I've done this successfully by using Copy on a command prompt and it works just fine. Using ReadData() and WriteData() within a program is not working. Can anyone tell me why this is? It puts the files together, but when you use a media player on the file, it halts after the first segment of whatever you put together.

Code: Select all

Procedure.l mod(num.l,div.l)

  ProcedureReturn num - num / div * div
EndProcedure

#BUFFERSIZE = 2048
#FILE_OUT = 0
#FILE_IN  = 1

NewList jfiles.s()

   temp_item.s = OpenFileRequester("Select files","","",0,#PB_Requester_MultiSelection)
   
   If temp_item
      AddElement(jfiles())
      jfiles() = Chr(27)
      
      Repeat
         AddElement(jfiles())
         jfiles() = temp_item
         temp_item = NextSelectedFileName()
      Until temp_item = ""
      
      *buffer = AllocateMemory(0,#BUFFERSIZE,0)
      
      If *buffer
         save_file.s = SaveFileRequester("Enter joined file name",GetPathPart(jfiles()) + "joined","",0)
         If save_file
            If IsFilename(GetFilePart(save_file) + GetExtensionPart(save_file))
               save_flag.b = #TRUE
            Else
               save_flag.b = #FALSE
               MessageRequester("Error","Illegal file name...",0)
            EndIf
         EndIf
         
         If FileSize(save_file) > -1
            Select MessageRequester("Alert!","File already exists. Overwrite?",#PB_MessageRequester_YesNo)
               Case 7 ;* no button *
                  save_flag = #FALSE
            EndSelect
         EndIf
         
         If FileSize(save_file) = -2
            MessageRequester("Error","The name you have chosen is a directory...",0)
            save_flag = #FALSE
         EndIf
         
         If save_flag
            If CreateFile(#FILE_OUT,save_file)
               Repeat
                  If ReadFile(#FILE_IN,jfiles())
                     UseFile(#FILE_IN)
                     i = Int(Lof() / #BUFFERSIZE)
                     write_count = 0
                     If i > 0
                        Repeat
                           UseFile(#FILE_IN)
                           ReadData(*buffer,#BUFFERSIZE)
                           UseFile(#FILE_OUT)
                           WriteData(*buffer,#BUFFERSIZE)
                           write_count + 1
                        Until write_count = i
                     EndIf
                     UseFile(#FILE_IN)
                     tail_length = mod(Lof(),#BUFFERSIZE)
                     ReadData(*buffer,tail_length)
                     UseFile(#FILE_OUT)
                     WriteData(*buffer,tail_length)
                     CloseFile(#FILE_IN)
                  Else
                     MessageRequester("Error","Unable to open " + jfiles() + "...",0)
                  EndIf
                  PreviousElement(jfiles())
               Until jfiles() = Chr(27)
               
               CloseFile(#FILE_OUT)
               ClearList(jfiles())
               FreeMemory(0)
               MessageRequester("Message","Done",0)
            Else
               MessageRequester("Error","Unable to create joined file...",0)
            EndIf
         EndIf
      Else
         MessageRequester("Error","Unable to allocate memory...",0)
      EndIf
   EndIf
tomk
New User
New User
Posts: 5
Joined: Wed Jun 11, 2003 4:37 pm

Post by tomk »

Hi urland,

I tested the code with (easier to read) big textfiles and it seems to work fine.

Maybe there is a problem with "0"Bytes (Hex 00) in readdata/writedate, chr(26) may be a problem too ?
Is the filesize of the merged binary files exacltly matching the sum of the single files ?
To debug I would create files containing just lines with a "aaa" / "bbbb" and a running number and look to the merged result. Then I would create a file with a "0" (chr(26) Bytes inside.
I would be glad if you post a solution if you find one, I will someday do a filemerger for my .MPG files (cut at 2 gigs)

Greetings
tom
urland
User
User
Posts: 13
Joined: Tue May 13, 2003 6:53 pm

Post by urland »

Oops! My mistake entirely. I had NOT tested mpg files with Copy from a command prompt. I tested mp3 files which work fine. The routine posted works fine on mp3's as well. I guess I'll have to try it with all the media formats I can to see what types can be combined into a larger file and playback all the way through.

Urland --------> :oops:
Post Reply