Get Video Duration with FFMPEG and Python

For this to work, you’ll need FFMPEG and Python on your machine or server already. Configuration of this is beyond the scope of this post, but installation through yum or apt-get should be sufficient (or equivalent on a Windows or Mac). To pull the duration of a video from any machine with FFMPEG and Python installed, run the following script. Take care to replace PATH_TO_YOUR_VIDEO_FILE with the path to your video and check to ensure your ffmpeg binary is located at /usr/bin/ffmpeg (you can do this by executing ‘which ffmpeg’ in your shell). The result is a dictionary with hours, minutes and seconds as the keys.

import subprocess
import re
 
process = subprocess.Popen(['/usr/bin/ffmpeg',  '-i', PATH_TO_YOUR_VIDEO_FILE], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = re.search(r"Duration:\s{1}(?P\d+?):(?P\d+?):(?P\d+\.\d+?),", stdout, re.DOTALL).groupdict()
 
print matches['hours']
print matches['minutes']
print matches['seconds']

Update:

Cal Leeming was kind enough to put this in a function that’ll return the number of seconds as a Decimal. Thanks again, Cal!

import subprocess
import re
from decimal import Decimal
 
def get_video_length(path):
	process = subprocess.Popen(['/usr/bin/ffmpeg', '-i', path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	stdout, stderr = process.communicate()
	matches = re.search(r"Duration:\s{1}(?P\d+?):(?P\d+?):(?P\d+\.\d+?),", stdout, re.DOTALL).groupdict()
 
	hours = Decimal(matches['hours'])
	minutes = Decimal(matches['minutes'])
	seconds = Decimal(matches['seconds'])
 
	total = 0
	total += 60 * 60 * hours
	total += 60 * minutes
	total += seconds
	return total

FizzBuzz in 68k Assembly

Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

	ORG	$1000
FIZZ	DC.B	'FIZZ',$0D,$0A,0
BUZZ	DC.B	'BUZZ',$0D,$0A,0
FBUZZ	DC.B	'FIZZBUZZ',$0D,$0A,0
CRLF	DC.B    $0D,$0A,0
 
START
	MOVEQ 	#0, D1		; Rolling value
	MOVEQ	#10, D2		; Set D2 with dec number 10
	BSR	LOOP		; Branch to loop
 
QUIT	MOVE.B  #9, D0		; Moves the dec number 9 into data register D0
        TRAP    #15		; Execute trap command in D0
 
DBUZZ	LEA     BUZZ, A1	; Load BUZZ address into address register A1
        MOVE.B  #14, D0		; Set trap command to display NULL terminated string in A1
        TRAP    #15		; Execute trap command 
	BRA	LOOP		; Branch to LOOP
 
DFIZZ	LEA     FIZZ, A1	; Load FIZZ address into address register A1
        MOVE.B  #14, D0		; Set trap command to display NULL terminated string in A1
        TRAP    #15		; Execute trap command 
	BRA	LOOP		; Branch to LOOP
 
DFBUZZ	LEA     FBUZZ, A1	; Load FBUZZ address into address register A1
        MOVE.B  #14, D0		; Set trap command to display NULL terminated string in A1
        TRAP    #15		; Execute trap command 
	BRA	LOOP		; Branch to LOOP
 
DVAL	MOVEQ	#15, D0		; Set trap command to display D1
	TRAP	#15		; Execute trap command
	LEA     CRLF, A1	; Load FBUZZ address into address register A1
        MOVE.B  #14, D0		; Set trap command to display NULL terminated string in A1
        TRAP    #15		; Execute trap command
	BRA	LOOP		; Branch to LOOP
 
LOOP	CMP	#100, D1	; Compare D1 to 100
   	BEQ	QUIT		; Equality in previous compare meets break condition
 
	ADDQ	#1, D1		; Increment loop	
	MOVE.L	D1, D3		; Store D1 to D3 for DIV
 
	DIVU	#15, D3		; D3 / 15
	SWAP	D3		; Swap D3 remainder byte	
	CMP	#0, D3		; Evaluate D3 for 0
	BEQ	DFBUZZ		; Run subroutine to display 'FIZZBUZZ'
 
	MOVE.L	D1, D3		; Store D1 to D3 for DIV
	DIVU	#3, D3		; D3 / 3
	SWAP	D3		; Swap D3 remainder byte	
	CMP	#0, D3		; Evaluate D3 for 0
	BEQ	DFIZZ		; Run subroutine to display 'FIZZ'
 
	MOVE.L	D1, D3		; Store D1 to D3 for DIV
	DIVU	#5, D3		; D3 / 5
	SWAP	D3		; Swap D3 remainder byte	
	CMP	#0, D3		; Evaluate D3 for 0
	BEQ	DBUZZ		; Run subroutine to display 'BUZZ'
 
	BRA	DVAL		; Other conditions not met, branch into DVAL
 
	END	START

Arduino Default Fuse Settings

Here are the default fuse settings for each Arduino from the boards.txt included with the Arduino development software.

To understand more about the fuse settings for your microcontroller, visit Engbedded’s AVR Fuse Calculator.

To write fuse settings, you will need a programmer with this capability. I use Mighty Ohm’s high-voltage rescue shield available here.

Arduino Uno

Low Fuse 0xFF
High Fuse 0xDE
Extended Fuse 0x05

Arduino Duemilanove or Nano w/ ATmega328

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0x05

Arduino Diecimila, Duemilanove, or Nano w/ ATmega168

Low Fuse 0xFF
High Fuse 0xDD
Extended Fuse 0x00

Arduino Mega 2560

Low Fuse 0xFF
High Fuse 0xD8
Extended Fuse 0xFD

Arduino Mega (ATmega1280)

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0xF5

Arduino Mini

Low Fuse 0xFF
High Fuse 0xDD
Extended Fuse 0x00

Arduino Fio

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0x05

Arduino BT w/ ATmega328

Low Fuse 0xFF
High Fuse 0xD8
Extended Fuse 0x05

Arduino BT w/ ATmega168

Low Fuse 0xFF
High Fuse 0xDD
Extended Fuse 0x00

LilyPad Arduino w/ ATmega328

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0x05

LilyPad Arduino w/ ATmega168

Low Fuse 0xE2
High Fuse 0xDD
Extended Fuse 0x00

Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0x05

Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168

Low Fuse 0xFF
High Fuse 0xDD
Extended Fuse 0x00

Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328

Low Fuse 0xFF
High Fuse 0xDA
Extended Fuse 0x05

Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168

Low Fuse 0xC6
High Fuse 0xDD
Extended Fuse 0x00

Arduino NG or older w/ ATmega168

Low Fuse 0xFF
High Fuse 0xDD
Extended Fuse 0x00

Arduino NG or older w/ ATmega8

Low Fuse 0xDF
High Fuse 0xCA

Generate Random IP with Python

In need of an IP address on-the-fly that appears to be valid? Try this:

from random import randrange
 
def generateIP():
    blockOne = randrange(0, 255, 1)
    blockTwo = randrange(0, 255, 1)
    blockThree = randrange(0, 255, 1)
    blockFour = randrange(0, 255, 1)
    print 'Random IP: ' + str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)
    if blockOne == 10:
        return self.__generateRandomIP__()
    elif blockOne == 172:
        return self.__generateRandomIP__()
    elif blockOne == 192:
        return self.__generateRandomIP__()
    else:
        return str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)

We’re skipping 10.x.x.x, 172.x.x.x and 192.x.x.x due to the fact that these are reserved address. RFC 1918

Version 2:

An elegant solution to serve the purpose of generating a random IP provided by Ben (explanation of changes listed in the comments below):

if __name__=="__main__":
    not_valid = [10,127,169,172,192]
 
    first = randrange(1,256)
    while first in not_valid:
    first = randrange(1,256)
 
    ip = ".".join([str(first),str(randrange(1,256)),
    str(randrange(1,256)),str(randrange(1,256))])
    print ip