           console.log pos,size
           if pos==size
             console.log 'downloaded'
-            @convertToGif.apply @,[ytid]
+            @insertVideo.apply @,[savePath]
         video.pipe fs.createWriteStream(savePath)
       else
-        @convertToGif ytid
+        @insertVideo savePath
     else
       @removeVideo()
 

      @blurImage()
      @elements.videoPath=atom.packages.resolvePackagePath('editor-background')+
        '/youtube-videos/'
      try
        fs.mkdirSync @elements.videoPath,0o777
      catch error

      if ytidregres?.length>0
        ytid=ytidregres[1]

  timer:{}
  gif:{}

  capture: ->
    video = @elements.video
    console.log "capturing"
    @gif.addFrame video, {copy: true, delay: 100}

  decodeVideo:->
    video = @elements.video
    canvas = @elements.videoCanvas

    @gif = new GIF
      workers: 4
      workerScript: @elements.libPath+'gif.worker.js'
      width: 640
      height: 480
      quality: 1

    @gif.on 'progress', (p) ->
      console.log "rendering: #{ Math.round(p * 100) }%"

    @gif.on 'finished', (blob) =>
      dataURL = URL.createObjectURL(blob)
      console.log 'gif finished',dataURL,blob
      blobToBase64 blob, (base64)=>
        videoPath = @elements.videoPath
        ytid = @getYTId atom.config.get('editor-background.youTubeURL')
        try
          fs.writeFileSync videoPath+ytid+'.gif',base64,'base64'
        catch error
          console.log error

    video.addEventListener 'play', =>
      console.log 'playing'
      clearInterval @timer
      @timer =
      setInterval =>
        @capture.apply @,[]
      , 100

    video.addEventListener 'ended', =>
      clearInterval @timer
      @gif.render()

    @gif.abort()
    @gif.frames = []
    video.play()

  insertVideo: (savePath) ->
    data = fs.readFileSync savePath
    ###
    videoCanvas = document.createElement 'canvas'
    videoCanvas.widht = "100%"
    videoCanvas.height = "100%"
    videoCanvas.id = "editor-background-videoCanvas"
    videoCanvas.style.cssText = "
    position:absolute;
    top:0px;
    left:0px;
    width:100%;
    height:100%;
    "
    @elements.videoCanvas = videoCanvas
    @elements.main.insertBefore videoCanvas,@elements.textBackground
    ###
    video = document.createElement 'video'
    source = document.createElement 'source'
    @elements.video =  video
    @elements.source = source
    video.appendChild source
    source.type="video/"+@elements.videoFormat
    source.src = savePath
    video.width="100%"
    video.height="100%"
    video.style.cssText="
    position:absolute;
    left:0;
    top:0;
    width:100%;
    height:100%;
    "
    @elements.main.insertBefore video,@elements.textBackground
    @decodeVideo()

  downloadYTVideo: (url)->
    videoExt = @elements.videoExt
    videoFormat = @elements.videoFormat
    if url != ''
      ytid = @getYTId url
      savePath = @elements.videoPath+ytid+videoExt
      alreadyExists = false

      try
