Myth2psp

From MythDora

Jump to: navigation, search

PSP_Myth.png

Contents

Myth2psp

This page is for those of you who wish to automatically convert and publish your recorded TV programmes for viewing on your Sony PSP with firmware 1.1 and above (or itunes). In fact any device that can play movie files and understand RSS feeds will work.

I have tested the script on the following :

  1. Firefox (Linux, Windows)
  2. Sharp Reader (Windows)
  3. Sony PSP
  4. Nokia N800

The parameters in the script which determine the type and quality of the transcoding start with:

$FFMPEGBIN 
VIDEO_BITRATE 
FRAME_RATE
VIDEO_RESOLUTION

The default command is for the Sony PSP and transcodes from a UK DVB (Digital Freeview) MPEG source. Change to suit - if you get stuck for the perfect command line, remember Google is your friend.

This script is not as advanced as the myth2ipod script but it is quick and easy to get going. Also, I am not the original author of the script - I found it on the net but it was broken. I have made many changes, and obtained permission from the original author.

Limitations

  1. The script will not delete the transcoded files. You will have to do it manually.
  2. Transcoded programmes are placed into a folder and an xml file created. However if there is already an episode in the folder then the xml will be overwritten so you only see the most recent version via RSS even though the old video file is still there. (I'll change this shortly and update the wiki)

Files Used For RSS Feed

  1. myth2psp bash script
  2. podcast.php
  3. PSP_Myth.png

So lets get started...


We cannot have our media files stored in /var/www/html folder because an automatic Mythdora install creates quite a small partition by default. Instead, we will create a folder in /var/www/html called PSP and our script will automatically create links in the PSP directory back to /storage/psp which should have tons of room since /storage is where all the programmes are saved by default.

As root

#mkdir /storage/psp
#mkdir /var/www/html/PSP
#chown apache:apache /storage/psp /var/www/html/PSP

Now copy the below script into your /usr/bin directory and call it myth2psp (don't forget to make it executable!)


myth2psp

#!/bin/bash

FILE="/storage/recordings/$1"
TITLE="$2"
SUBTITLE="$3"
DESCRIPTION="$4"
CATEGORY=$5
PROGSTART=$6
# CHANGE BELOW LINE TO YOUR DOMAIN NAME
URL="http://www.example.com/PSP"

#MYTHTV JOB USAGE
#/usr/bin/myth2psp.sh "%FILE%" "%TITLE%" "%SUBTITLE%" "%DESCRIPTION%" "%CATEGORY%" "%STARTTIME%"

FFMPEGBIN=/usr/bin/ffmpeg

MAINDIR="/var/www/html/PSP/"

OUTDIR="/storage/psp/$TITLE"
declare -i FILENUM=`cat $MAINDIR/.filenumber`
PUBDATE=`/bin/date -R`


#Make the directory
mkdir "$OUTDIR"

OUTFILE="M4V`cat $MAINDIR/.filenumber`.mp4"
OUTTHM="M4V`cat $MAINDIR/.filenumber`.THM"

echo "Encoding" > "$OUTDIR/.$OUTFILE.log"
echo "Input file: $FILE" >> "$OUTDIR/.$OUTFILE.log"
echo "Output file: $OUTFILE" >> "$OUTDIR/.$OUTFILE.log"
echo "Title: $TITLE" >> "$OUTDIR/.$OUTFILE.log"

let FILENUM=$FILENUM+1
printf "%05d" $FILENUM > $MAINDIR/.filenumber

#PARAMETERS
#FRAME_RATE = 29.97
VIDEO_BITRATE=768k;
VIDEO_RESOLUTION="320x240";
FRAME_RATE=14.985;
#VIDEO_BITRATE=160k;
#VIDEO_RESOLUTION="368x208";

# make sure the below command is on one line and dosn't wrap or you may get an error!!!
$FFMPEGBIN  -y -i $FILE -title "$TITLE" -f psp -s $VIDEO_RESOLUTION -r $FRAME_RATE -b $VIDEO_BITRATE -acodec aac -ac 2 -ar 24000 -ab 64k -map 0:0 -map 0:1 "$OUTDIR/$OUTFILE" >> "$OUTDIR/.$OUTFILE.log"
#$FFMPEGBIN -v -y -i $FILE -title "$TITLE" -bitexact -f psp -s $VIDEO_RESOLUTION -r $FRAME_RATE -b $VIDEO_BITRATE -acodec aac -ac 2 -ar 24000 -ab 32 -map 0:0 -map 0:1 "$OUTDIR/$OUTFILE" >> "$OUTDIR/.$OUTFILE.log"
$FFMPEGBIN -y -i $FILE -title "$TITLE" -s 160x90 -padtop 16 -padbottom 14 -r 1 -t 1 -ss 3:00.00 -an -f mjpeg "$OUTDIR/$OUTTHM" >> "$OUTDIR/.$OUTFILE.log"

#Lets find the file size of our new Podcast file 
SIZE=$(stat -c%s "$OUTDIR/$OUTFILE")
# | /bin/cut -f1`


#OUTPUT THE PODCAST XML
printf "<item>\n" > "$OUTDIR/podcast.xml"
printf "<title>" >> "$OUTDIR/podcast.xml"
printf "$TITLE - $SUBTITLE" | sed "s/&/&/g" >> "$OUTDIR/podcast.xml"
printf "</title>\n" >> "$OUTDIR/podcast.xml"
printf "<link>$URL/$TITLE/$OUTFILE</link>\n" >> "$OUTDIR/podcast.xml"
printf "<itunes:author>MythTV</itunes:author>\n" >> "$OUTDIR/podcast.xml"  
printf "<author>MythTV</author>\n" >> "$OUTDIR/podcast.xml"  
printf "<itunes:category text=\"TV Shows\"></itunes:category>\n" >> "$OUTDIR/podcast.xml"
printf "<comments>$FILE</comments>\n" >> "$OUTDIR/podcast.xml" 
printf "<description>" >> "$OUTDIR/podcast.xml"
printf "$DESCRIPTION" | sed "s/&/&/g" >> "$OUTDIR/podcast.xml"
printf "</description>\n" >> "$OUTDIR/podcast.xml"
printf "<pubDate>$PUBDATE</pubDate>\n" >> "$OUTDIR/podcast.xml" 
printf "<enclosure url=\"" >> "$OUTDIR/podcast.xml"
printf "$URL/$TITLE/$OUTFILE" | sed "s/ /%20/g" >> "$OUTDIR/podcast.xml"
printf "\" length=\"" >> "$OUTDIR/podcast.xml"
printf "$SIZE" >> "$OUTDIR/podcast.xml"
printf "\" type=\"video/mp4\" />\n" >> "$OUTDIR/podcast.xml"
printf "<itunes:duration></itunes:duration>\n" >> "$OUTDIR/podcast.xml"
printf "<itunes:keywords>$TITLE - $SUBTITLE - $CATEGORY</itunes:keywords>\n" >> "$OUTDIR/podcast.xml"
printf "<category>$CATEGORY</category>\n" >> "$OUTDIR/podcast.xml"		
printf "</item>\n" >> "$OUTDIR/podcast.xml"

#change perms
chown -R apache:apache "$OUTDIR"
ln -s "/storage/psp/$TITLE" "/var/www/html/PSP/$TITLE"


OK, so the above file is the engine for the RSS feeder. It gets run after a programme finishes recording via a user job. Line 13 in the above file tells you the syntax for the user job. You need to add that via the mythtv GUI (dont forget to enable it!) or via the settings table in mythweb.

The script itself creates a folder (if it doesn't exist) in /storage/psp/, then transcodes the recorded files, creates an xml file and saves both files to the folder.


podcast.php

Now copy the file below into the contents of a file called podcast.php

<?php
	header("Content-Type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
?> 
<rss xmlns:itunes="http://www.itunes.com/DTDs/Podcast-1.0.dtd" version="2.0">
	<channel>
		<title>
			MythTV - TV Shows
		</title>
		<itunes:author>
			MythTV - myth2psp
		</itunes:author>
		<link>http://www.example.com/PSP/podcast.php</link>
		<itunes:subtitle>
			Transcoded recording for your PSP.
		</itunes:subtitle>
		<itunes:summary>
			Myth TV Recorded Programs for the PSP v.1 and above
		</itunes:summary>
		<description>
			Myth TV Recorded Programs for the PSP v.1 and above
		</description>
		<itunes:owner>
			<itunes:name>
				Jo Bloggs
			</itunes:name>
			<itunes:email>
				Jo.Bloggs\@example.com
			</itunes:email>
		</itunes:owner>
		<itunes:explicit>
			No
		</itunes:explicit>
		<language>
			en-uk
		</language>
		<copyright>
			Copyright for sale.
		</copyright>
		<webMaster>
			Jo.Bloggs\@example.com
		</webMaster>
		<itunes:image href="http://www.example.com/PSP/PSP_Myth.png" />
		<itunes:category text="TV Shows">
		</itunes:category>
		<category>
			TV Shows
		</category>
		<image>
			<url>
				http://www.example.com/PSP/PSP_Myth.png
			</url>
			<title>
				MythTV 2 Playstation Portable
			</title>
			<link>
			http://http://www.example.com/PSP/podcast.php
			</link>
			<width>
				200
			</width>
			<height>
				200
			</height>
		</image>
		<?
			$psp_directory = opendir(".");
			while($filename = readdir($psp_directory)){ 
				if(is_dir($filename)){
					if($filename != "." and $filename != ".." ) {
						$movie_directory = opendir("./$filename");
						while($podname = readdir($movie_directory)){
							if($podname == "podcast.xml"){
								//print("<file>$podname");
								//print("</file>");
								//print("./$filename/$podname");
								print file_get_contents("./$filename/$podname");
							}
						}
						
					}
				}
			}
closedir($psp_directory);
?>
	</channel>
</rss>

Wherever http://www.example.com appears within the script you need to change it with your own domain name. Unless your name is Jo Bloggs you want to change that as well. When changing the email address notice how the "@" symbol is escaped with a forward slash "\".

This file basically has all the syntax for a RSS feed except for the location and type of media. When it comes to the media it executes some PHP scripting which loops through the links pulling through the text in the xml files generated by the myth2psp user job.


PSP_Myth.png

Just about there...all we need is a pic! as root

cd /var/www/PSP
wget http://carrick.ndo.co.uk/PSP_Myth.png
chown apache:apache ./PSP_Myth.png


Apache

Now we need to tell apache not to go straight for mythweb.

I can't remember if I needed to change this or not so lets check its output.

cat /etc/httpd/conf/httpd.conf |grep "DocumentRoot \"/var"

If this returns

DocumentRoot "/var/www/html"

...then all is OK. If not, check to see if you can access www.example.com/PSP/podcast.xml by using firefox live bookmarks. If you get a password box flash up, try the below syntax instead (it should be the same user/password you use to access mythweb).

http://user_name:password@www.example.com/PSP/podcast.php

Note: you can actually access a directory listing by leaving off the file name above (podcast.php). You may not want this. To stop this behaviour you could always just create a "dummy" index.html file with no content.


<html><head>
<title>No Access Allowed</title> </head><body>
No Access Allowed
</body></html>


Enjoy

NZcarrick

Personal tools