How to get video files duration and resolution in python

Hi all,

Today i was about to find video file duration and resolution and few more info in python.

In debain based systems type this command in terminal

avconv

If this shows version , like this

avconv version 9.18-6:9.18-0ubuntu0.14.04.1, Copyright (c) 2000-2014 the Libav developers
built on Mar 16 2015 13:19:10 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
Hyper fast Audio and Video encoder
usage: avconv [options] [[infile options] -i infile]… {[outfile options] outfile}…

Use -h to get full help or, even better, run ‘man avconv’

If it says commond or package not found, then install this by

sudo apt-get install libav-tools

After successful installation, In terminal

avconv -i "filepath"  # give path to your video file

You will see output like this
Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: mp42mp41
    creation_time   : 2015-10-21 16:41:08
  Duration: 00:00:30.00, start: 0.000000, bitrate: 748 kb/s
    Stream #0.0(eng): Video: h264 (Constrained Baseline), yuv420p, 480×240, 488 kb/s, 25 fps, 25 tbr, 25 tbn
    Metadata:
      creation_time   : 2015-10-21 16:41:08
    Stream #0.1(eng): Audio: aac, 48000 Hz, stereo, fltp, 255 kb/s
    Metadata:
      creation_time   : 2015-10-21 16:41:08

You can see the resolution and duration highlighted my result.

Now lets see how to use this in python to get video resolution, duration.

The idea is simple , am gonna use subprocess and pipe the output

Here is the python code.


from subprocess import Popen, PIPE
import re

def getvideodetails(filepath):
    cmd = "avconv -i %s" % filepath
    p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    di = p.communicate()
    for line in di:
        if line.rfind("Duration") > 0:
            duration = re.findall("Duration: (\d+:\d+:[\d.]+)", line)[0]
        if line.rfind("Video") > 0:
            resolution = re.findall("(\d+x\d+)", line)[0]
    return duration,resolution

# call function with file path
getvideodetails("filepath") 

This code can be found in gist too here

Thats it.

Happy coding!!!
Happy times !!!

Advertisement

Hello World in Django !!!!

Hi …After long time i ‘m blogging about django..Lets start our journey to django .Before reading this i would suggest to get some knowledge in python .

My suggestions for python learning  are

  1. A byte of python with pdf
  2. Dive into Python
  3. Learn python the Hard way

What is Django?

Django is a Web framework written in python language .

what is meant by frame work?

After searching about framework, Frameworks are similar to libraries or templates that are already written for you, you can re-use the code to build your system . i.e(Collection of codes that uses some control mechanisum).

still didn’t get???

I explain with my view, lets forget about all technical terms and others. Lets say you want to build your new house ?

To build new house what are the things you needed?

  • money
  • stones
  • cement
  • empty ground (place)
  • water
  • sand and others stuffs etc….

now same thing for building your system(project)

Framework offers everything you needed, the only thing you need to do is ,place the things in correct manner. like(database details in proper place, your logics , representation of your data. etc.)

Hope you understand something !!!!

now coming to our django framework.It follows MVC pattern like Ruby on Rails  .

MVC: => Model View Controller

Model: for Database access.(this contains database,table details)

Views: your logic goes here

Templates: To represent your data(html files)

Why MVC or What is the Advantage of this?

The biggest Advantage of MVC is ,one’s change doesn’t affect other.

for example if you want to change your database details(say db name or table details) you need to change in model file only( no need of find and replace in entire project).Because each and everything are loosely coupled(independent). Same thing applies for view and templates too.

didn’t get it??

while doing sample code i will explain it …

To install django see install

Sorry i ‘m not going to tell about installation . Since i’m an linux user I know about installation in linux only. You can find lot of source for your operating system installations.

Django documentation is fair enough for any os.

After installing ,lets create the sample project.

Its good practice to create working directory for learning any language .

open terminal(cntrl+alt+T)

create directory mkdir djcodes(here djcodes is my directory name ,you can give any name)

cd djcodes(change to working directory)

To start a new django project (i’m using django 1.5 version):

django-admin startproject sample # here sample is a project name

change directory to sample

cd sample/

now issue  ls command ,

manage.py      sample

You have manage.py file and sample folder inside sample project .

manage.py – points to the settings file in your project. (This files are automatically created by django)

Inside sample folder you have 4 files

__init__py  – this will indicate your project as python package to compiler.

urls.py – file to hold the urls of your website .(e.x)http://localhost/hello

here in order to use /hello in our project you have to mention this in urls.py.

(see my below explanation for these concepts)

settings.py – File that will hold all apps,database settings of your information .

(if you open this file means you can see,time zone,templates etc..). You will learn more about these files in my upcoming posts.

Wsgi.py– This file handles our requests and responses .(our django development server)

Ok. Lets start the server by

./manage.py runserver (Note inside project directory)

this is show like this

Validating models…

0 errors found
Django version 1.4.5, using settings ‘sample.settings’
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Jan/2014 11:45:00] “GET / HTTP/1.1” 200 1957

Open your favorite browser and see the link   http://127.0.0.1:8000/

It worked!

Congratulations on your first Django-powered page.

Of course, you haven’t actually done any work yet. Here’s what to do next:

  • If you plan to use a database, edit the DATABASES setting in sample/settings.py.
  • Start your first app by running python manage.py startapp [appname].

You’re seeing this message because you have DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!

This is default page in django .

Our goal is Hello world page in django .

Lets Start creating our app and display hello world

To create an app(inside project directory)

django-admin startapp hello

This will create a hello folder in project directory.It has four files

__init__.py  – this file indicates your app as python package.

models.py – file to hold your database informations

views.py – your functions to hold requests,logics etc.

tests.py – for testing purposes.

Three things to do for our task

1. add url in urls.py with associate function.

2. write code for url in urls.py

3. html file to render a response.

Lets add url in urls.py

gedit  /sample/urls.py

add your like this

from django.conf.urls import patterns, include, url
from hello.views import myfunction

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'sample.views.home', name='home'),
    # url(r'^sample/', include('sample.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'hello/$',myfunction),
)

my views.py(In hello/views.py)

# Create your views here.
from django.http import HttpResponse
def myfunction(request,):
	return HttpResponse("Hello")

save your files .
now run server ./manage.py runserver

Now see in browser by typing http://localhost:8000/hello

this will return hello in page . Thats it our goal is done .

What happens behind the scene . I will explain shortly.

when you type localhost:8000/hello —> this will send request to django

this is will read urls.py and look for pattern and url match . if it matches it calls the associated function.

In our case it matches hello in urls.py and calls myfunction in views.py.

In views.py –> we have the code to display the hello as HttpResponse .

Thats it .. Thanks for reading… Happy coding !!!

Scrapy Beginning Tutorial

Hi to all. Today i started learning scrapy .Here i’m going to start scrapy from the beginning ..

What is Scrapy?

Scrapy is an application framework for crawling websites and extracting  structured data which can be used for a wide range of useful applications, like data mining, information processing etc.

Take a look at the documentation of scrapy  for more information here

Scrapy was written in Python. Hence you must some knowledge in python to work in scrapy.

For those who are beginners in python i would suggest these books “A Byte of Python ”  & “Learning Python the HardWay” (or) “Dive into Python“.

For those who have already knowledge in python remember this . Scrapy supports python 2.6 and 2.7 . Scrapy doesn’t support Python3.

Lets see the installation here.

Pre requisites :

  • Python 2.7
  • lxml
  • Opensssl
  • Pip or easy_install (Python package Managers)

To install Scrapy Open Terminal and type(cntrl + Alt + T)

$ pip install Scrapy

or

$ easy_install Scrapy

After installation type  $ scrapy

Scrapy 0.18.4 – no active project

Usage:
  scrapy <command> [options] [args]

Available commands:
  bench         Run quick benchmark test
  fetch         Fetch a URL using the Scrapy downloader
  runspider     Run a self-contained spider (without creating a project)
  settings      Get settings values
  shell         Interactive scraping console
  startproject  Create new project
  version       Print Scrapy version
  view          Open URL in browser, as seen by Scrapy

  [ more ]      More commands available when run from project directory

Use “scrapy <command> -h” to see more info about a command

It shows the installed scrapy version and other details .
Thats it .. From my next post we will get started with Coding …

Happy Times !!! Thanks !!!!!!!!

 

How to fix subtitles delay or ealier with your movies by python code

Hi..all …today i was watching movie with subtitles . I had delay with my subtitles files ,like  subtitles mismatch with  the every scenes of movie.  .

I sloved this issue by python code .Subtitles are in .srt format. (e.x) Pirates of the Caribbean -The Curse of the Black Pearl(2003).srt

I found that i got delay by 2 minutes in my .srt file

Library that  i used : pysrt

Open your terminal (cntrl+alt+t)

type python —> enter into python interpreter mode

import pysrt

if you got any error like –> no moduled named pysrt.  then you need to install pysrt .To install pysrt – sudo easy_install pysrt

for python3 users its available pysrt3

if don’t get any error then you already have this library.

now , if you want to make a delay in .srt file means do like this

 

>>> subs.shift(seconds=-2) # Move all subs 2 seconds earlier
>>> subs.shift(minutes=1)  # Move all subs 1 minutes later

finally save this file from your terminal by

>>> subs.save('path to ur location/newfilename.srt', encoding='utf-8')

My Entire code which sloved my delay in .srt file

#! usr/bin/python
import pysrt
subs=open("/home/bala/Pirates of the Caribbean -The Curse of the Black Pearl(2003).srt")
subs.shift(minutes=-2) # Move all subs 2 minutes earlier
subs.save('/home/bala/new.srt', encoding='utf-8')#saves file with new.srt in your home directory

This sloved my problem .
Thats it..   Happy coding !!!!

Beginning with pygame

Hi…to all …today i learnt how to begin with pygame in python

now lets consider getting intensity range of RED,GREEN ,BLUE  inputs from user  and display the resultant colour in window.

 

#! usr/bin/python
#color.py
import pygame
from pygame.locals import *
from sys import exit
 
r=min(255, input("Enter the red colour intensity  :"))
g=min(255, input("enter the green colour intensity : "))
b=min(255, input("enter the blue colour intensity  :"))
screen=pygame.display.set_mode((640,480),0,24)
pygame.display.set_caption("colour testing ")
 
while True:
 for i in pygame.event.get():
  if i.type==QUIT:
   exit()
 screen.fill((r, g, b))
 pygame.display.update()

Run the file by python color.py

Window will appear with the resultant color …

Thats it…enjoy coding with pygame….:) Thanks …..:)

How to get IP address in Python

Hi..to all …today i learnt how to get IP address in python..

To get the Hostname

import socket
print socket.gethostname()

To get the IP Address

import socket
print socket.gethostbyname(socket.gethostname())

we can also check with dummy socket and see the host name

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
s.getsockname()[0]

Thats it…:)

How to delete files in python

Hi…to all today i learnt how to delete files in python..here the codes

import os
os.remove("Myfilename.txt")


suppose if you want to delete all the file named myfile1.txt,myfile2.txt…it goes on like this.. we can use myfile*.txt  to delete all those files..here the code for this

 

import os,glob
for filePath in glob.glob("Mydirectory/Myfile*.txt")
      if os.filePath.isfile(filePath):
         os.remove(filePath)

if you want to delete all the files in subdirectories you can use the following code

import os, glob
 
for root, dirs, files in os.walk('MyDirectory'):
    for filePath in glob.glob(os.path.join(root, "MyFile*.txt")):
        if os.path.isfile(filePath):
            os.remove(filePath)

it is possible to delete files you didn’t intend to. So make sure you test thoroughly before implementing code like this..

Thats it…thanks….happy coding with python…:)

How to download a image in a webpage in python

Hi..to all ..today i learnt how to download a image in a webpage in python…here the code is..

we need to copy the url of the image and give that in our code ..

#! usr/bin/python
import urllib2
import webbrowser
import os
# find yourself a picture on a web page you like
# (right click on the picture, look under properties and copy the address)
picture_page=”http://1.bp.blogspot.com/-_l6saamIXzk/T1hikYw-bUI/AAAAAAAAD7I/073fMnOIcwQ/s640/bhavana-stills-86-473×500.jpg&#8221;# give the url of the image to download
#webbrowser.open(picture_page) # test
# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 = opener1.open(picture_page)
my_picture = page1.read()
# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = “my_image” + picture_page[-4:]
print filename # test
fout = open(filename, “wb”)
fout.write(my_picture)
fout.close()
# was it saved correctly?
# test it out …
webbrowser.open(filename)
# or …
# on Windows this will display the image in the default viewer
#os.startfile(filename)

Run the file by Python filename.py

output:

Thats it…Thanks…..:)