예제 #1
0
class SWFOutputStream(MovieOutputStream):
    """
  SWFOutputStream is a MovieOutputStream which produces a SWF file.
  """

    swf_version = None

    def __init__(self, info, debug=0):
        assert info.filename, 'Filename not specified!'
        MovieOutputStream.__init__(self, info, debug)
        self.info.set_swf_version(self.swf_version)
        self.writer = None
        self.cursor_depth = None
        self.cursor_pos0 = None
        return

    def open(self):
        MovieOutputStream.open(self)
        print >>stderr, 'Creating movie: %r: version=%d, size=%dx%d, framerate=%s, compression=%s' % \
              (self.info.filename, self.info.swf_version,
               self.info.width, self.info.height,
               self.info.framerate, self.info.compression)
        self.writer = SWFWriter(
            self.info.filename, self.swf_version,
            (0, self.info.width * 20, 0, self.info.height * 20),
            self.info.framerate, self.info.compression)
        # Write BGColor
        self.writer.start_tag()
        self.writer.writergb((255, 255, 255))
        self.writer.end_tag(9)
        # add mp3 header (if any)
        if self.info.mp3:
            # write SoundStreamHeader
            assert self.info.mp3.isstereo != None, 'mp3 isstereo is not set.'
            assert self.info.mp3.sample_rate != None, 'mp3 sample_rate is not set.'
            self.writer.start_tag()
            MP3_RATE = {11025: 1, 22050: 2, 44100: 3}
            rate = MP3_RATE[self.info.mp3.sample_rate]
            self.writer.writeui8(rate << 2 | 2 | int(self.info.mp3.isstereo))
            self.writer.writeui8(rate << 2 | (2 << 4) | 2
                                 | int(self.info.mp3.isstereo))
            self.writer.writeui16(
                int(self.info.mp3.sample_rate / self.info.framerate))
            # the first seeksamples, mp3.seek_frame should be preformed in advance.
            self.writer.writeui16(self.info.mp3.seeksamples)
            self.writer.end_tag(18)
        # Make the movie unscalable.
        if not self.info.scalable:
            # add actionscript: Stage.scaleMode("noScale")
            self.writer.start_action()
            self.writer.do_action(
                0x96, 7)  # PushData, size= 1 (type) + len(data) + 1 (0x00)
            self.writer.writeui8(0x00)  # String
            self.writer.writestring('Stage')
            self.writer.do_action(0x1c)  # GetVariable
            self.writer.do_action(0x96, 11)  # Push
            self.writer.writeui8(0x00)  # String
            self.writer.writestring('scaleMode')
            self.writer.do_action(0x96, 9)  # Push
            self.writer.writeui8(0x00)  # String
            self.writer.writestring('noScale')
            self.writer.do_action(0x4f)  # setMember
            self.writer.end_action()

        self.othertags = []
        return

    def write_mp3frames(self, frameid=None):
        # add mp3 frames (if any)
        if frameid == None:
            frameid = self.output_frames
        if self.info.mp3:
            t = (frameid + 1) / self.info.framerate
            (nsamples, seeksamples,
             mp3frames) = self.info.mp3.get_frames_until(t)
            # SoundStreamBlock
            self.writer.start_tag()
            self.writer.writeui16(nsamples)
            self.writer.writeui16(seeksamples)
            self.writer.write(''.join(mp3frames))
            self.writer.end_tag(19)
        return

    def define_shape(self, w, h, data, alpha=False):
        if self.debug:
            print >> stderr, 'define_shape:', (w, h), len(data)
        self.writer.start_tag()
        image_id = self.writer.newid()
        self.writer.writeui16(image_id)
        self.writer.writeui8(5)  # fmt=5: 24bits color
        self.writer.writeui16(w)
        self.writer.writeui16(h)
        self.writer.write(zlib.compress(data))
        if alpha:
            # DefineBitsLossless2
            self.writer.end_tag(36,
                                forcelong=True)  # because of flashplayer's bug
        else:
            # DefineBitsLossless
            self.writer.end_tag(20,
                                forcelong=True)  # because of flashplayer's bug
        # DefineShape3
        self.writer.start_tag()
        shape_id = self.writer.newid()
        self.writer.writeui16(shape_id)
        self.writer.writerect((20, w * 20 + 20, 20, h * 20 + 20))
        self.writer.write_style(3, [(0x43, None, None, None, image_id,
                                     (20, 20, None, None, 0, 0))], [])
        self.writer.write_shape(3, [(0, (20, 20)), (1, (w * 20, 0)),
                                    (1, (0, h * 20)), (1, (-w * 20, 0)),
                                    (1, (0, -h * 20))],
                                fillstyle=1)
        self.writer.end_tag(32)
        return shape_id

    def place_object2(self, shape_id, x, y, depth):
        if self.debug:
            print >> stderr, 'place_object2:', shape_id, (x, y), depth
        # PlaceObject2
        self.writer.start_tag()
        if shape_id:
            self.writer.writeui8(2 | 4)
            self.writer.writeui16(depth)
            self.writer.writeui16(shape_id)
        else:
            self.writer.writeui8(1 | 4)
            self.writer.writeui16(depth)
        self.writer.writematrix((None, None, None, None, x * 20, y * 20))
        self.writer.end_tag(26)
        return

    # remove shape object
    # if you leave objects on the screen, it gets verry slow.
    def remove_object(self, depth):
        if self.debug:
            print >> stderr, 'remove_object:', depth
        # RemoveObject2
        self.writer.start_tag()
        self.writer.writeui16(depth)
        self.writer.end_tag(28)
        return

    # paint cursor
    def paint_frame(self, (images, othertags, cursor_info)):
        MovieOutputStream.paint_frame(self, (images, othertags, cursor_info))
        self.othertags.extend(othertags)
        return
예제 #2
0
파일: output.py 프로젝트: GunioRobot/castro
class SWFOutputStream(MovieOutputStream):

  """
  SWFOutputStream is a MovieOutputStream which produces a SWF file.
  """

  swf_version = None

  def __init__(self, info, debug=0):
    assert info.filename, 'Filename not specified!'
    MovieOutputStream.__init__(self, info, debug)
    self.info.set_swf_version(self.swf_version)
    self.writer = None
    self.cursor_depth = None
    self.cursor_pos0 = None
    return

  def open(self):
    MovieOutputStream.open(self)
    print >>stderr, 'Creating movie: %r: version=%d, size=%dx%d, framerate=%s, compression=%s' % \
          (self.info.filename, self.info.swf_version,
           self.info.width, self.info.height,
           self.info.framerate, self.info.compression)
    self.writer = SWFWriter(self.info.filename, self.swf_version,
                            (0,self.info.width*20, 0,self.info.height*20),
                            self.info.framerate, self.info.compression)
    # Write BGColor
    self.writer.start_tag()
    self.writer.writergb((255,255,255))
    self.writer.end_tag(9)
    # add mp3 header (if any)
    if self.info.mp3:
      # write SoundStreamHeader
      assert self.info.mp3.isstereo != None, 'mp3 isstereo is not set.'
      assert self.info.mp3.sample_rate != None, 'mp3 sample_rate is not set.'
      self.writer.start_tag()
      MP3_RATE = {11025:1, 22050:2, 44100:3}
      rate = MP3_RATE[self.info.mp3.sample_rate]
      self.writer.writeui8(rate << 2 | 2 | int(self.info.mp3.isstereo))
      self.writer.writeui8(rate << 2 | (2<<4) | 2 | int(self.info.mp3.isstereo))
      self.writer.writeui16(int(self.info.mp3.sample_rate / self.info.framerate))
      # the first seeksamples, mp3.seek_frame should be preformed in advance.
      self.writer.writeui16(self.info.mp3.seeksamples)
      self.writer.end_tag(18)
    # Make the movie unscalable.
    if not self.info.scalable:
      # add actionscript: Stage.scaleMode("noScale")
      self.writer.start_action()
      self.writer.do_action(0x96,7) # PushData, size= 1 (type) + len(data) + 1 (0x00)
      self.writer.writeui8(0x00) # String
      self.writer.writestring('Stage')
      self.writer.do_action(0x1c) # GetVariable
      self.writer.do_action(0x96, 11) # Push
      self.writer.writeui8(0x00) # String
      self.writer.writestring('scaleMode')
      self.writer.do_action(0x96, 9) # Push
      self.writer.writeui8(0x00) # String
      self.writer.writestring('noScale')
      self.writer.do_action(0x4f) # setMember
      self.writer.end_action()
    
    self.othertags = []
    return

  def write_mp3frames(self, frameid=None):
    # add mp3 frames (if any)
    if frameid == None:
      frameid = self.output_frames
    if self.info.mp3:
      t = (frameid+1) / self.info.framerate
      (nsamples, seeksamples, mp3frames) = self.info.mp3.get_frames_until(t)
      # SoundStreamBlock
      self.writer.start_tag()
      self.writer.writeui16(nsamples)
      self.writer.writeui16(seeksamples)
      self.writer.write(''.join(mp3frames))
      self.writer.end_tag(19)
    return

  def define_shape(self, w, h, data, alpha=False):
    if self.debug:
      print >>stderr, 'define_shape:', (w,h), len(data)
    self.writer.start_tag()
    image_id = self.writer.newid()
    self.writer.writeui16(image_id)
    self.writer.writeui8(5)             # fmt=5: 24bits color
    self.writer.writeui16(w)
    self.writer.writeui16(h)
    self.writer.write(zlib.compress(data))
    if alpha:
      # DefineBitsLossless2
      self.writer.end_tag(36, forcelong=True) # because of flashplayer's bug
    else:
      # DefineBitsLossless
      self.writer.end_tag(20, forcelong=True) # because of flashplayer's bug
    # DefineShape3
    self.writer.start_tag()
    shape_id = self.writer.newid()
    self.writer.writeui16(shape_id)
    self.writer.writerect((20, w*20+20, 20, h*20+20))
    self.writer.write_style(3, [(0x43,None,None,None,image_id,(20,20,None,None,0,0))], [])
    self.writer.write_shape(3, [(0,(20,20)),(1,(w*20,0)),(1,(0,h*20)),(1,(-w*20,0)),(1,(0,-h*20))], fillstyle=1)
    self.writer.end_tag(32)
    return shape_id

  def place_object2(self, shape_id, x, y, depth):
    if self.debug:
      print >>stderr, 'place_object2:', shape_id, (x,y), depth
    # PlaceObject2
    self.writer.start_tag()
    if shape_id:
      self.writer.writeui8(2|4)
      self.writer.writeui16(depth)
      self.writer.writeui16(shape_id)
    else:
      self.writer.writeui8(1|4)
      self.writer.writeui16(depth)
    self.writer.writematrix((None, None, None, None, x*20, y*20))
    self.writer.end_tag(26)
    return
  
  # remove shape object
  # if you leave objects on the screen, it gets verry slow.
  def remove_object(self, depth):
    if self.debug:
      print >>stderr, 'remove_object:', depth
    # RemoveObject2
    self.writer.start_tag()
    self.writer.writeui16(depth)
    self.writer.end_tag(28)
    return

  # paint cursor
  def paint_frame(self, (images, othertags, cursor_info)):
    MovieOutputStream.paint_frame(self, (images, othertags, cursor_info))
    self.othertags.extend(othertags)
    return
예제 #3
0
class SWFOutputStream(MovieOutputStream):
    """
  SWFOutputStream is a MovieOutputStream which produces a SWF file.
  """

    swf_version = None

    def __init__(self, info, debug=False):
        assert info.filename, 'Filename not specified!'
        MovieOutputStream.__init__(self, info, debug)
        self.info.set_swf_version(self.swf_version)
        self.writer = None
        return

    def open(self):
        MovieOutputStream.open(self)
        print >>stderr, 'Creating movie: %r: version=%d, size=%dx%d, framerate=%s, compression=%s' % \
              (self.info.filename, self.info.swf_version,
               self.info.width, self.info.height,
               self.info.framerate, self.info.compression)
        self.writer = SWFWriter(
            self.info.filename, self.swf_version,
            (0, self.info.width * 20, 0, self.info.height * 20),
            self.info.framerate, self.info.compression)
        # Write BGColor
        self.writer.start_tag()
        self.writer.writergb((255, 255, 255))
        self.writer.end_tag(9)
        # add mp3 header (if any)
        if self.info.mp3:
            # write SoundStreamHeader
            assert self.info.mp3.isstereo != None, 'mp3 isstereo is not set.'
            assert self.info.mp3.sample_rate != None, 'mp3 sample_rate is not set.'
            self.writer.start_tag()
            MP3_RATE = {11025: 1, 22050: 2, 44100: 3}
            rate = MP3_RATE[self.info.mp3.sample_rate]
            self.writer.writeui8(rate << 2 | 2 | int(self.info.mp3.isstereo))
            self.writer.writeui8(rate << 2 | (2 << 4) | 2
                                 | int(self.info.mp3.isstereo))
            self.writer.writeui16(
                int(self.info.mp3.sample_rate / self.info.framerate))
            # the first seeksamples, mp3.seek_frame should be preformed in advance.
            self.writer.writeui16(self.info.mp3.seeksamples)
            self.writer.end_tag(18)
        self.othertags = []
        return

    def next_frame(self):
        MovieOutputStream.next_frame(self)
        # add other unknown tags
        for (tag, data) in self.othertags:
            self.writer.start_tag()
            self.writer.write(data)
            self.writer.end_tag(tag)
        # ShowFrame
        self.writer.start_tag()
        self.writer.end_tag(1)
        self.othertags = []
        return

    def write_mp3frames(self, frameid=None):
        # add mp3 frames (if any)
        if frameid == None:
            frameid = self.output_frames
        if self.info.mp3:
            t = (frameid + 1) / self.info.framerate
            (nsamples, seeksamples,
             mp3frames) = self.info.mp3.get_frames_until(t)
            # SoundStreamBlock
            self.writer.start_tag()
            self.writer.writeui16(nsamples)
            self.writer.writeui16(seeksamples)
            self.writer.write(''.join(mp3frames))
            self.writer.end_tag(19)
        return

    def close(self):
        MovieOutputStream.close(self)
        if self.writer:
            self.writer.start_tag()
            self.writer.end_tag(0)
            self.writer.write_file(self.output_frames)
            self.writer = None
        return