quinta-feira, 24 de maio de 2012

ffmpeg - convertendo vídeos para webm

ffmpeg -i videofinal.mkv -codec:v libvpx -quality good -cpu-used 0 -b:v 1200k -threads 4 videofinal.webm

ffmpeg - screencast

ffmpeg -f alsa -ac 2 -i pulse -f x11grab -s hd1080 -r 60 -i :0.0 -vcodec libx264 -preset ultrafast -an -y video.mkv -acodec pcm_s16le -b:a 48000k -loglevel quiet -vn -y audio.mkv

sábado, 3 de março de 2012

Sobre o Problema Da Soma De Sub-Ítens Do noskleta

Referente ao post do nosklo http://pythonlog.wordpress.com/2010/01/19/problema-da-soma-de-sub-itens/ O código a seguir consegue executar todos os testes de dados_teste.txt em menos de 2 segundos em um processador x4 955 overclock 4ghz
from itertools import chain, combinations
       
itens = range(-2000, 1, 1) + range(1,10, 3)
a = sorted([i for i in itens if i > 0], reverse=True)
b =  sorted([i for i in itens if i < 0], reverse=True)
found = False
totalB = abs(sum(b))
 
if sum(a) < totalB:
    a, b = b, a
    totalB = abs(sum(b))            
   
       
if b != [] or a != []:
  
    if len(a) < 30:
 
        for i in chain.from_iterable(combinations(a, n+1)
                           for n in xrange(len(b),0,-1)):
        
            if abs(sum(i)) - totalB == 0:
                currentList = a+b
                sublist =  list(i)+b
                absoluteValue= (abs(sum(i)) + totalB)
                print  "com %s retornou %s Cujo o Valor absoluto é: %s  " \
                                  % (currentList,  sublist,  absoluteValue)
                found = True
                break
         
    else:
 
        loop = True
        size = len(a)
         
        while loop and size >= 2:       
   
            for i in chain.from_iterable(combinations(a, n+1)
                                 for n in xrange(size,0,-1)):
   
                if sum(abs(j) for j in i) >=  totalB:
                    size-=1
                    break
                else:
                    loop = False
                break
             
        for i in chain.from_iterable(combinations(a, n+1)
                                  for n in xrange(size,0,-1)):
        
            if abs(sum(i)) - totalB == 0:
                currentList = a+b
                sublist =  list(i)+b
                absoluteValue= (abs(sum(i)) + totalB)
                print  "com %s retornou %s Cujo o Valor absoluto é: %s  " \
                          % (currentList,  sublist,  absoluteValue)
                found = True
                break

    if not found:
        for i in chain.from_iterable(combinations(itens, n+1)
                         for n in xrange(len(itens), 0, -1)):
            if abs(sum(i)) == 0:
                currentList = itens
                sublist =  list(i)
                absoluteValue= sum( abs(j) for j in i )
                print  "com %s retornou %s Cujo o Valor absoluto é: %s  " \
                                  % (currentList,  sublist,  absoluteValue)
                found = True
                break
        
   
    if not found:     
        print "%s Nenhuma sublista" % itens

quinta-feira, 6 de outubro de 2011

smtplib - Enviando E-mails com anexo

def email():
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import formatdate
    from email import Encoders
    import os
    user = "user@email.com" 
    password = "password" 
    subject = "email teste" 
    fromaddr = 'from@email.com' 
    toaddrs = 'to@email.com' 
    msg = MIMEMultipart()
    msg['Date'] = formatdate( localtime = True )
    
    msg['Subject'] = 'subject'

    msg.attach( MIMEText( 'oi' ) )
    part = MIMEBase( 'application', "octet-stream" )
    open( 'teste.txt', 'w' ).write( 'teste')
    part.set_payload( open( 'teste.txt', "rb" ).read() )
    Encoders.encode_base64( part )
    part.add_header( 'Content-Disposition', 'attachment; filename="teste.txt"' )
    msg.attach( part )
        
    servidor = smtplib.SMTP( 'smtp.domain:port' )
    servidor.ehlo()  
    servidor.starttls()  
    servidor.ehlo()  
    servidor.login( user, password )  
    servidor.sendmail( fromaddr, toaddrs, msg.as_string() )

quarta-feira, 5 de outubro de 2011

Pyramid - convertendo html para pdf

import cStringIO as StringIO
import ho.pisa as pisa
from pyramid.renderers import render


def pdf( request ):
    
    request.response.content_type = 'application/pdf'

    request.response.headerlist.append( ( 'Content-Disposition', 'attachment; filename=test.pdf' ) )
    
    value = {}
    
    html = render( 'nameoftheproject:templates/foobar.jinja2', value, request )
    
    result = StringIO.StringIO()
    
    pisa.pisaDocument( StringIO.StringIO( html.encode( "UTF-8" ) ), result )
    
    request.response.write( result.getvalue() )

    return request.response

quinta-feira, 9 de junho de 2011

python - armazenando a saída de programas com subprocess

outputCMD = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
outputCMD = outputCMD.stdout.readlines()