You are not logged in.

#1 2008-02-03 00:47:16

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

audio-recorder.py - Record from Line-In, Mic, etc...

I wrote a script to record audio from the alsa capture device (Line-In, Mic, etc...)

The script is called audio-recorder.py
Website: http://pyther.net/audio-recorder.php

What it is
A small script I put together so I could record a radio show for my father on my linux server. I plan on updating and improving the script whenever I have time.

How to use
-Configure alsa so it captures from the device you want to record from (line-in, mic, etc...)
-Pass parameters to the script audio-recorder.py --help (for more information)
-Setup a crontab job when you want the script to start recording (if you want this to happen automatically)

[Depends]
-sox
-mp3splt
-alsa-utils

Download: http://pyther.net/audio-recorder/audio- … r-0.2.5.py

#Pyther <pyther[at]pyther[dot]net>
#!/usr/bin/env python

import os     #for running applications
import time    #use for getting the current time
import subprocess
import sys
import datetime
import optparse

#bash stuff
#arecorder.py -H 20 -M 50 -d /home/steve/`date +%B.%d.%Y-%a` -f Howard-`date +%m%d%y`.mp3 -t 10.00 -c 2
#creates dated directory /home/steve/`date +%h.%d.%y-%a`
#creates dated file  Howard-`date +%m%d%y`.mp3
#-d /home/steve/`date +%h.%d.%y-%a` -f Howard-`date +%m%d%y`.mp3

if __name__=="__main__":
    parser = optparse.OptionParser("usage: %prog [options]")
    parser.add_option("-H", "--hour", dest="endhour",
                      type="int", help="end time hour")

    parser.add_option("-M", "--minute", dest="endminute",
              type="int", help="end time minute")

    parser.add_option("-l", "--shour", dest="starthour",
                      type="int", help="start time hour")

    parser.add_option("-m", "--sminute", dest="startminute",
              type="int", help="end time minute")

    parser.add_option("-d", "--dir", dest="dir",
              default='/home/steve/', type="string", help="Directory where recording will go")

    parser.add_option("-r", "--samplerate", dest="samplerate",
              default='48000', type="int", help="Sample Rate")

    parser.add_option("-t", "--splittime", dest="splittime",
              default='0', type="float", help="Spilt Time (ex. 10.00) for 10 minutes")

    parser.add_option("-c", "--channels", dest="channels",
              default='2', type="int", help="Channels (ex. 2 for stero, or 1 for mono)")

    parser.add_option("-f", "--filename", dest="filename",
              default='default.mp3', type="string", help="Name of the file, make sure this includes .mp3!")

    parser.add_option("-w", "--warning", dest="warning",
              default="True", type="string", help="Displays warning that it is going to over write dir, and file, False or True")

    parser.add_option("-g", "--startnow", dest="startnow",
              type="string", help="determines if we should skip waiting for the begining time, for web.py")
   

    (options, args) = parser.parse_args()
    
    STARTHOUR = options.starthour
    STARTMINUTE = options.startminute
    ENDHOUR = options.endhour
    ENDMINUTE = options.endminute
    DIR = options.dir
    SAMPLERATE=options.samplerate
    SPLITTIME=options.splittime
    CHANNELS=options.channels
    FILENAME=options.filename
    WARNING=options.warning
    startnow=options.startnow

print 'This program records from the line-in, Mic, etc...'

#print startnow
#time.sleep(100)

if STARTHOUR==None:
    print ' no hour'
    startnow='true'
elif STARTMINUTE==None:
    print 'err .. no minute'
    startnow='true'
elif startnow=='true':
    pass
else:
       startnow='false'

#DATE=time.strftime("%h.%m.%y-%a")            #The Date as so Feb.02.08-Mon

print 'Doing some prep work....'

#DIRPATH=DIR+DATE+'/'
DIRPATH=DIR+'/'
FILEPATH=DIRPATH+FILENAME


if os.path.isdir(DIRPATH):
    if WARNING=='True':
        print 'Directory Exists, this might be an error!'
else:
    os.mkdir(DIRPATH)

if WARNING=='True' and os.path.isfile(FILEPATH):
    print 'File Exits.... Will overwrite in 10 seconds!'
    for i in range(10, 0, -1):
        print str(i) + '...',
        sys.stdout.flush()
        time.sleep(1)
        
now=datetime.datetime.now()

if startnow=='true':
    STARTHOUR=now.hour
    STARTMINUTE=now.minute

filename = "/tmp/audio-recorder-" + str(os.getpid()) + ".tmp" #creat file with python pid
#Python pid, filename, start hour, start minute, end hour, end minute
FILE = open(filename,"w")
FILE.write('#PythonPID#Filename#shour#sminute#endhour#endminute\n')
FILE.writelines(str(os.getpid()) + '\t' + FILENAME + '\t' + str(STARTHOUR) + '\t' + str(STARTMINUTE) + '\t' + str(ENDHOUR) + '\t' + str(ENDMINUTE) + '\n')
FILE.close()
        
#set up date objects
#now=datetime.datetime.now()
if startnow=='false':
    start=datetime.datetime(now.year, now.month, now.day, STARTHOUR, STARTMINUTE)

#if startnow=='true':
#    print 'We are not going to wait for a start time!'
#    STARTHOUR=now.hour
#    STARTMINUTE=now.minute
if startnow=='false':
    if now.hour > STARTHOUR:    #if the hour is greater then when the recording it suppose to start it must end the following day, add a day to this
        start = start + datetime.timedelta(1)

    diff=start-now    #start should always be greater than now
    print 'start sleeping for: ' + str(diff.seconds) #debug stuff
    time.sleep(diff.seconds)

    
print 'Starting the record...'

pipe=subprocess.Popen(['/usr/bin/rec', '-q','-c',str(CHANNELS),'-r',str(SAMPLERATE),FILEPATH])
print "The pid is: " + str(pipe.pid)


FILE = open(filename,"w")
#Python PID, PID
FILE.write('#PythonPID#PID#Filename#shour#sminute#endhour#endminute\n')
FILE.write(str(os.getpid()) + '\t' + str(pipe.pid) + '\t' + FILENAME + '\t' + str(STARTHOUR) + '\t' + str(STARTMINUTE) + '\t' + str(ENDHOUR) + '\t' + str(ENDMINUTE) + '\n')
FILE.close()

now=datetime.datetime.now()
end=datetime.datetime(now.year, now.month, now.day, ENDHOUR, ENDMINUTE)
if now.hour > ENDHOUR:             #if the hour is greater then when the recording it suppose to end it must be the following day
    end = end + datetime.timedelta(1)

diff=end-now

print 'end sleeping for: ' + str(diff.seconds)
time.sleep(diff.seconds)

print 'Killing REC Process now!'
os.kill(pipe.pid, 9)
pipe.poll()

print 'Record process has been killed!'

print 'Splitting the mp3...'
split=subprocess.Popen(['/usr/bin/mp3splt', '-t',str(SPLITTIME),'-d',DIRPATH,FILEPATH])
split.poll()
print 'Done splitting mp3'

print 'Revmoving temp file...'
os.remove(filename)

Uses about 60 megabytes an hour

Example:

audio-recorder.py -i 06 -m 30 -H 7 -M 30 -d /home/recording -f test.mp3 -c 2

#Starts Recording at 6:30AM ends recording at 7:30AM with a filename of /home/recording/test.mp3

*Special thanks to sandsmark and everyone else who put up with my stupid bash questions in #archlinux tongue

Feel free to ask my any questions....

EDIT: Rewrote the script in python, update information above

Last edited by pyther (2008-02-11 05:01:05)


Website - Blog - arch-home
Arch User since March 2005

Offline

#2 2008-02-03 17:54:42

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: audio-recorder.py - Record from Line-In, Mic, etc...

Version 2:
*Added SAMPLE RATE (not all systems default to 48000)
*Fixed TIP, you can mute Analog Mix not line-in, my mistake


Website - Blog - arch-home
Arch User since March 2005

Offline

#3 2008-02-04 05:30:43

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: audio-recorder.py - Record from Line-In, Mic, etc...

nice work smile


I need real, proper pen and paper for this.

Offline

#4 2008-02-11 05:01:25

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: audio-recorder.py - Record from Line-In, Mic, etc...

UPDATE: Rewrote the script in python
Version 0.2.5 Released

Last edited by pyther (2008-02-11 05:01:45)


Website - Blog - arch-home
Arch User since March 2005

Offline

#5 2008-02-12 11:36:22

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: audio-recorder.py - Record from Line-In, Mic, etc...

For those of you who tried to download the script, I made a stupid mistake in my server config, thus you were getting the output of the script, but I fixed so now you'll actually get the code if you go to http://pyther.net/audio-recorder/audio- … r-0.2.5.py

Sorry


Website - Blog - arch-home
Arch User since March 2005

Offline

Board footer

Powered by FluxBB