sexta-feira, 6 de novembro de 2015

Save radio shows in podcasts so you can listen later!

The story behind this project is that, since I moved to Germany I miss some old news shows on the radio that I use to listen in Brazil. Yeah, I know I can listen to them online. But due to the difference on the timezone, sometimes I just don't want to listen to it while I have my lunch, or work. Saving them into a podcast is much easier and gives me the freedom to listen whenever I want.


The idea is pretty simple: Use ffmpeg to save the stream online, copy it to your podcast server and enjoy. Let's work to put everything together:

First thing you're gonna need is some scripts. The first one captures the stream of audio and dumps into a ts file:

otubo@deathstar /opt/ $ cat capture_stream.sh 
#/bin/bash

TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S");
/usr/bin/ffmpeg -i URL -c copy /opt/radio_show_${TIMESTAMP}.ts

Next, you're going to need a script to encode the transport stream into mp3:

otubo@deathstar /opt/ $ cat encode_stream.sh 
#/bin/bash

LAST_TS_FILE="$(ls -1t /opt/|grep ts|head -1)"
NEW_MP3_FILE="$(echo $LAST_TS_FILE|sed -e 's/ts/mp3/g')"
LAST_TS_FILE="/opt/${LAST_TS_FILE}"
NEW_MP3_FILE="/var/www/html/media/${NEW_MP3_FILE}"
/usr/bin/ffmpeg -i "${LAST_TS_FILE}" -acodec mp3 -write_xing 0 "${NEW_MP3_FILE}"
/usr/bin/id3 -t "$(date)" "${NEW_MP3_FILE}"
/usr/bin/id3 -a "News" "${NEW_MP3_FILE}"
wget -O- http://GENERATOR SERVER ADDR/html/pg-cron.php?key=YOUR KEY >/dev/null 2>&1
rm "${LAST_TS_FILE}"

Little gotcha: I had this issue with the iPhone podcast app, according to this ticket, adding the option -write_xing 0 solves the problem.

Note that in the last script, there's the "Podcast Generator" server. That's a really neat and simple to use podcast server made with php5. You can download it here: http://podcastgen.sourceforge.net/. The instructions to install and configure are very easy, and as they say: Newbie-proof.

Let's dig down a little bit on the last script: First you run ffmpeg to convert the transport stream into mp3, then you set the title id3 tag for the title of this podcast "episode", than set the artist id3 tag for the description. After that you call wget to reload your podcast library and update the RSS. And it's done!

Now let's put everything on the crontab:

30 10 * * * /opt/capture_stream.sh
2  12 * * * killall -9 ffmpeg
3  12 * * * /opt/encode_stream.sh

Easy and simple: The show starts at 10:30 (my germany time) and ends at 12h00 (I added 2 more minutes, just in case). Three minutes after the dump is finished I start the encoding script. And that's all! Podcast generator also gives you RSS url to put on your smartphone. Pretty easy :D

RetroPie: Play snes games on your Raspberry Pi!

So I wanted to play some old Super Nintendo games and also share this special retro gaming style with my daughter. So I decided to put it on my Raspberry Pi and have some fun. So let's do it!

The process is pretty easy, but the controllers configuration are a little tricky. Follow this guide to install RetroPie on an SD card and boot up your Raspberry Pi. This documentation explains everything you need to know about configuring wifi, setting up everything to have all up and running.

Now to the controller configuration:
I bought two ordinary snes controllers, they work pretty fine. The configuration is done via the file /opt/retropie/configs/all/retroarch.cfg, find this file and open it with your favorite text editor. Now find the input_player and erase everything related to it and make it look like this:

input_device_p1 = "0"
input_libretro_device_p1 = "0"
input_player2_analog_dpad_mode = "0"
input_player1_joypad_index = "0"

input_player1_b_btn = "2"
input_player1_y_btn = "3"
input_player1_select_btn = "8"
input_player1_start_btn = "9"
input_player1_up_axis = "-1"
input_player1_down_axis = "+1"
input_player1_left_axis = "-0"
input_player1_right_axis = "+0"
input_player1_a_btn = "1"
input_player1_x_btn = "0"
input_player1_l_btn = "4"
input_player1_r_btn = "5"


input_device_p2 = "1"
input_libretro_device_p2 = "0"
input_player2_analog_dpad_mode = "0"
input_player2_joypad_index = "1"

input_player2_b_btn = "2"
input_player2_y_btn = "3"
input_player2_select_btn = "8"
input_player2_start_btn = "9"
input_player2_up_axis = "-1"
input_player2_down_axis = "+1"
input_player2_left_axis = "-0"
input_player2_right_axis = "+0"
input_player2_a_btn = "1"
input_player2_x_btn = "0"
input_player2_l_btn = "4"
input_player2_r_btn = "5"

input_enable_hotkey_btn = "8"

input_exit_emulator_btn = "9"

This is all you're going to need to make those USB controllers work. If you bought a different controller, you can try to configure them using this command:

sudo ./retroarch-joyconfig -j 1 -p 2 >> /opt/retropie/configs/all/retroarch.cfg

This will interactively ask you to push every button in order to map it to the correct values. Always remember to double check your /opt/retropie/configs/all/retroarch.cfg to avoid duplicates, this got me in trouble for quite some time.

After that, you can copy all your ROM files into /home/pi/RetroPie/roms/snes, or if you have ROMs for different console, just copy into the correct folder.

That's it. Enjoy :D