Saturday, February 23, 2008

Downloading and ripping music from YouTube videos

Uses


  • music videos

  • speeches

  • news stories

  • video podcasts

  • etc.

Ways to download YouTube videos

  • On Debian-based systems,

    sudo apt-get install youtube-dl; youtube-dl http://youtube.com/watch?v=foobar123

    The best reason to use youtube-dl is so that you can automate this process to do batch downloads.

  • Use a web-based tool

  • The easiest way is to let your browser do it for you. Go to the video page, and wait until your browser downloads it. Then check /tmp on *nix systems with Firefox (not sure about other browsers) for a file like FlashXXXXX where X is an alphanumeric character. This is the downloaded FLV video.

  • Another solution for anyone using Firefox is to look in the disk cache (about:cache?device=disk) and look for an entry starting with http://www.youtube.com/get_video?video_id=foobar123&t=foobar456. Clicking on this entry will show you its location on disk e.g. /home/yourusername/.mozilla/firefox/xx4xmys7.default/Cache/FB999693d01. The main benefit of using this method is that Firefox will show you the direct URL of the video on the web: http://lax-v305.lax.youtube.com/get_video?video_id=BUNCHOFTEXTHERE

  • Look through the site html and javascript and figure out the flv URL. jk :)


Ways to extract the audio from the file

  • Use vixy.net. Just type in the youtube URL and select mp3 from the dropdown.

  • Use ffmpeg -i youtubevid.flv youtubemusic.mp3

  • Use mplayer -dumpaudio youtubevid.flv -dumpfile youtubemusic.mp3



Vixy.net method [via labnol.blogspot.com]

FFmpeg method [via linux.byexamples.com]

mplayer method [via tips.webdesign10.com] (also created a bash script to automate this)

Tuesday, December 25, 2007

Text to HTML Table Converter 1.2.1: Fixed flag bug in txt2html v1.2

The argsContain(flag) function in the script only checks if the first argument contains 'flag'. Replace argsContain with:


def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
found = False
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
found = True
return found



This checks all the flags for the one you want.

Saturday, November 24, 2007

Compiling packages from source on Ubuntu

A default Ubuntu install doesn't come with many compiling tools like GCC, G++, libc6-dev, and Make that you need to compile programs from source. To get all those packages, type in:

sudo apt-get install build-essential

You will need the Ubuntu CD for this.

Jump words with Control-Arrow in the Ubuntu terminal

On a fresh install of both Ubuntu 7.04 and 7.10, pressing Ctrl-[Arrow Key] does not jump words but instead writes a control code onto the screen such as ;5C or ;5D.

To fix this, add export INPUTRC=/etc/inputrc to /etc/bash.bashrc.

Make sure /etc/inputrc has these lines in it:

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

Tuesday, September 4, 2007

Make gnome-screensaver opaque with Compiz

If you use Compiz and GNOME, you might have noticed that you can still see your screen after you lock it. This is because, Compiz allows gnome-screensaver's window(the window that normally turns your screen black) to be slightly transparent. To fix this, go to the CompizConfig Settings Manager (most likely in System > Preferences), click on General Options and go to the Opacity Settings tab. You will see something like

((type=Menu | PopupMenu | DropdownMenu | Tooltip | Notification | Combo | Dnd | name=sun-awt-X11-XWindowPeer) | (type=Normal & override_redirect=1)) & !(name=sun-awt-X11-XFramePeer | name=sun-awt-X11-XDialogPeer)

This is a conditional statement which tells Compiz which windows to make transparent. You can either add name=gnome-screensaver to the opacity windows listbox and change the opacity to full or you can add it to the original item in the listbox to change it into:

((type=Menu | PopupMenu | DropdownMenu | Tooltip | Notification | Combo | Dnd | name=sun-awt-X11-XWindowPeer) | (type=Normal & override_redirect=1)) & !(name=sun-awt-X11-XFramePeer | name=sun-awt-X11-XDialogPeer | name=gnome-screensaver)


[via www.tedcarnahan.com]

Wednesday, August 22, 2007

Text to HTML Table Converter 1.2

Now has unbuffered reads and better flag support


Unbuffered Input Reading


I added an pseudo-unbuff_read() function to the program (pseudo because it is really a sentinel-terminated loop concatenating a buffered read to a string)

def unbuff_read(END):
""" Reads one char at a time from stdin until reaches a "stop" character """
str = ""

while 1: # no do-whiles or assignments-in-conditionals in python :`(
# Compare the end of the string (same length as END) to END
if str[ len(END) * -1 :] == END:
break
str += sys.stdin.read(1)

return str


The parameter that you pass into this function called END breaks out of the read loop and returns the str read in up to that point. The default END is '\n.\n' which is a newline followed by a period and then another newline. This syntax is the same as the one used to stop reading the message body(DATA) when talking to smtp mail servers.

To quit the program just type in the END variable by itself.



Better flag support


Function argsContain():

def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
return True
else:
return False


This function goes through each argument before the filename and searches for a specific flag like "f" or "v". The two instances that used to search for flags have been replaced with a call to this method enabling syntax like
$ python txt2html-table-1.2.py -vf input.txt

and

$ python txt2html-table-1.2.py -fv input.txt



Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under. Download Text to HTML Table (txt2html-table) Version 1.2



Debugging


I kept my debugging code which consists of a function called debug() and a commented call to it after main() to make it easier to play around with the code by just uncommenting the call(and maybe commenting main()) and putting code in the debug() function.

Friday, August 17, 2007

Text to HTML Table Converter 1.0

Background


I was recently creating an description for a videogame I was selling on eBay and I needed to convert some text into an HTML table. Since I also was learning Python at the same time, I figured I would make a Python program that would do the job.

Input text format


The input text needs to be at least one line of text or more(seperated by a newline '\n') with a colon seperating the <th> element on the left and the <td> element on the right. You could also have another delimiter separating the two elements by changing the tList = eachLine.split(':') line to split based on whatever character you want.


The general format of the input text file should be:


thElement1: tdElement1
thElement2: tdElement2
...


where the <th> and <td> elements can have spaces, tabs, and other characters in them as long as they aren't '\n' or ':'.

Output HTML Format


The output which is displayed in the console (stdout) and which can optionally be saved to a file has this general format:


<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>


Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under.
Download Text to HTML Table (txt2html-table) Version 1.0

Usage Instructions


First of all, you need to download Python on your computer. If you're using a *NIX system, it should already by installed. The basic syntax is

python txt2html-table-1.0.py [-f] [-v] input.txt

If you don't include any of the optional flags (-f or -v), it will output the text in the file and confirm if you want to convert it. Type "y" or "yes" without the quotes to confirm or any other character(s) to exit. Upon confirmation, it will convert it to an HTML table and output the results.


Example: With no extra options


$ python txt2html-table-1.0.py input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================


Other command line options/flags


Use:

$ python txt2html-table-1.0.py -f input.txt

to prompt for a filename at the end of a conversion to save the html output to. If you decide to not save the output, press ENTER when it prompts for a filename in order to quit.

Example: Use of -f flag


$ python txt2html-table-1.0.py -f input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================
Enter a filename (Hit ENTER to quit): output.html
Output saved to: output.html


Use:

$ python txt2html-table-1.0.py -v input.txt

for verbose output. It shows you what elements each line was split into.

Example: Use of -v flag


$ python txt2html-table-1.0.py -v input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
' thElement1: tdElement1 ' was split into ['thElement1', 'tdElement1']
' thElement2: tdElement2 ' was split into ['thElement2', 'tdElement2']
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================

You can combine these two flags in either order as long as they're between the python file (.py) and the input filename. However, you cannot type in "-fv" or "-vf".

Errors you might receive



  • "Please pass a filename to me" if you didn't supply any arguments to the program.

  • "File does not exist." if the last argument you gave was not a valid file. Could occur if you added the flags after the input filename.


To avoid other errors, make sure that the python interpreter is in your OS's path, the program is in the current directory, and each line in your input file has one colon separating the <th> and <td> elements.

Todo: Possible features in future versions



  • Allow for specifying the output filename as a parameter i.e. python txt2html-table-1.0.py -f output.html input.txt

  • Allow more than two elements per line i.e. all elements after the first would be extra <td> elements in the table
  • Allow unbuffered reading in of text from the command line(stdin)

  • Add better support for flags (allow -fv or -vf)


Alternatives


Use the Python DOM library to build the table instead of raw HTML-holding variables like tHead, tBody, and tTail


Tools


Used Quick Escape to escape the html output for inserting into this post.