Windows – Creating PSP Thumbnails Using VLC and the Command Line

Windows 8 Logo

In my previous post I detailed the Handbrake settings that I am using to convert YouTube (and other) videos to play on my Play Station Portable (PSP).

Creating thumbnails for these PSP videos is a “nice to have” – but it is time consuming if it is not automated.

PSP thumbnails are simple to understand – they are jpeg files (160 x 120 pixels) that have had their extension changed to .thm. Videos and thumbnails must share the same filename on the PSP. So for a video called my_video.mp4 we would create a thumbnail called my_video.thm.

After looking at a lot of blogs and the VLC documentation I came up with a command line script that created a single jpeg thumbnail for each mp4 file in the current working directory:

for %i in (*.mp4) do if NOT exist "%~ni_*.jpg" start /WAIT "" "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" --rate=2.0 --video-filter=scene --start-time=280 --stop-time=281 --scene-format=jpg --scene-replace --scene-ratio=18 --scene-width=160 --scene-height=120 --scene-path="%CD%" --scene-prefix="%~ni" "%CD%\%i" vlc://quit

The above command line is modified from this post. It can be broken down as follows:

  • for %i in (*.mp4): filters the media to process (in my case .mp4 files)
  • do if NOT exist “%~ni_*.jpg”: creates a jpeg snapshot (if it does not already exist)
  • start /WAIT “”: is required to only process one video at a time
  • “C:\Program Files (x86)\VideoLAN\VLC\vlc.exe”: This is the path to vlc.exe on Windows 8 x64.
  • –rate=2.0: sets the playback speed. I set this to 2.0 so that vlc plays the specified video segments at twice the normal speed.
  • –video-filter=scene: the scene video filter sends video to picture files
  • –start-time=280: the stream will start at this position (in seconds)
  • –stop-time=281: the stream will stop at this position (in seconds)
  • –scene-format=jpg: specifies the screenshot format
  • –scene-replace: always writes to the same output file. This is used so that I can obtain a single screenshot that only needs the extension to be renamed.
  • –scene-ratio=18: the ratio of images to record. Initially this was set to 24 (1 image every 24 frames) – however with a start and stop time interval of one second I found that some of the screenshots that vlc output were black screens. I lowered this value until I could consistently generate screenshots from multiple .mp4 files. Your mileage may vary.
  • –scene-width=160: output image width
  • –scene-height=120: output image height
  • –scene-path=”%CD%”: output image directory – %CD% specifies current working directory
  • –scene-prefix=”%~ni” “%CD%\%i”: output image name – in this case the same name as the .mp4 video and the same working directory
  • vlc://quit: Quit vlc

The above script works when it is pasted into a command prompt window – but it required me to navigate to the drive and directory that contained my videos – and I still needed to change the extension of the screenshots produced to .thm. So the next step was to modify the script so that it would run in a batch file.

d:
cd PSP

for %%i in (*.mp4) do if NOT exist "%%~ni_*.jpg" start /WAIT "" "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" --rate=2.0 --video-filter=scene --start-time=280 --stop-time=281 --scene-format=jpg --scene-replace --scene-ratio=18 --scene-width=160 --scene-height=120 --scene-path="D:\PSP" --scene-prefix="%%~ni" "D:\PSP\%%i" vlc://quit

ren *.jpg *.thm

The differences made between the first and second scripts are as follows:

  • d: changes drive from c: to d:
  • cd PSP: changes directory to PSP
  • single percentage signs % are all changed to double percentage signs %% to allow the script to run in a batch file
  • –scene-path=”D:\PSP: the output image directory is changes to D:\PSP
  • –scene-prefix=”%%~ni” “D:\PSP\%%i”: the output image name also specifies the D:\PSP directory
  • ren *.jpg *.thm: renames all jpeg files in D:\PSP to .thm files

Many thanks to ComCBoVudLaBa on the VLC forum for providing a script with supporting comments that does all of the heavy lifting required. After some experimentation and tweaking I ended up with a batch file that completely automated the creation of PSP thumbnails from .mp4 videos!

Sources:

Notepad ++ Unix Shell Scripts and the ‘No such file or directory’ error on Ubuntu Server

ubuntu-server-logo

I first encountered this issue a long time ago and forgot to document it – so now I had to resolve it again.

I’m running Notepad ++ on Windows and using WinSCP to transfer bash scripts to Ubuntu Server. My script is saved as an Unix script file (*.sh, *.bsh) in Notepad++ and WinSCP is set to transfer files in binary mode.

When I run the script on Ubuntu Server I get the following error:

: No such file or directory

I could see the problem in Notepad++ and in nano on Ubuntu Server when I wanted to save the file – the file was formatted for Dos\Windows.

dos-windows
Notepad ++ document format set to Dos\Windows
dos-format
Nano saving .sh as Dos Format

The fix is to change the formatting to UNIX. In Notepad ++ click Edit, EOL Conversion and UNIX and save the file.

Easy when you remember how!

Ubuntu – Change Hostname Permanently Using the Command Line

ubuntu-logo

On Ubuntu the hostname is stored in both the /etc/hosts and /etc/hostname files. There are several ways that we can change the hostname in these files.

1. Manually Edit the hostname

We can manually edit these files using a basic text editor like nano:

sudo nano /etc/hosts
sudo nano /etc/hostname

In /etc/hostname simply overwrite the existing hostname with a new one. In /etc/hosts you will find the hostname on the line beginning 127.0.0.1 – overwrite only the hostname with the new one, and then reboot.

Editing /etc/hosts using nano
Editing /etc/hosts using nano
sudo reboot

2. Use sed to change the hostname

Another way to achieve the same goal is to use the sed command to replace the existing hostname with a new one.

For example, my Ubuntu Server has the default hostname of ‘ubuntu’.

Use the hostname command to check what your hostname is.

With sed we can look for our hostname (in /etc/hosts and /etc/hostname) and then replace it with the desired new-hostname:

sudo sed -i 's/ubuntu/new-hostname/g' /etc/hosts
sudo sed -i 's/ubuntu/new-hostname/g' /etc/hostname

Reboot:

sudo reboot

3. Write a Bash Script

It’s always handy to have a script to do things – so here is a quick bash script that I put together that uses sed to change the hostname and then reboot:

#!/bin/bash
#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)

#Display existing hostname
echo "Existing hostname is $hostn"

#Ask for new hostname $newhost
echo "Enter new hostname: "
read newhost

#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname

#display new hostname
echo "Your new hostname is $newhost"

#Press a key to reboot
read -s -n 1 -p "Press any key to reboot"
sudo reboot

Ubuntu – Enabling Respositories Using The Command Line

Installing software or making system changes from the command line can be a great time saver. Editing the repositories list though has always been something that I have done manually – either opening /etc/apt/sources.list with nano or making the necessary changes via the GUI.

Today, for example, I wanted to install Skype in Ubuntu 11.4 which meant enabling the “partner” repository. In /etc/apt/sources.list this would mean manually removing the # from the following line:

# deb http://archive.canonical.com/ubuntu natty partner

With a bit of searching though I found a solution that can be adapted to enable any of the existing entries in sources.list.

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
STOP="deb "
REPO="partner"
sudo sed -i "/^# $STOP.*$REPO/ s/^# //" /etc/apt/sources.list

The first command simply makes a backup copy of your sources.list file.

In the second line we define the parameter STOP as “deb ” so that we will only edit deb repositories (and not deb-src repositories which are not needed to install Skype).

The third line defines the REPO parameter which in this case contains a character string that is unique to the ubuntu natty partner repository.

The last line uses the sed command to remove the # according to the parameters that we have set.

This script gives a great and easily customizable framework to work from as STOP will either be “deb ” or “deb-src” and REPO will always be something unique to the repository to be enabled.

Kudos to Franklin52 on the The Unix and Linux Forums for the script.