<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>®om&#039;s blog &#187; psp</title>
	<atom:link href="http://blog.rom1v.com/tag/psp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rom1v.com</link>
	<description>Un blog libre</description>
	<lastBuildDate>Sun, 05 Sep 2010 17:23:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>pspxconv : script d&#8217;encodage de vidéos pour PSP</title>
		<link>http://blog.rom1v.com/2008/10/pspxconv-script-dencodage-de-videos-pour-psp/</link>
		<comments>http://blog.rom1v.com/2008/10/pspxconv-script-dencodage-de-videos-pour-psp/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 17:37:00 +0000</pubDate>
		<dc:creator>®om</dc:creator>
				<category><![CDATA[Outils]]></category>
		<category><![CDATA[planet-libre]]></category>
		<category><![CDATA[puf]]></category>
		<category><![CDATA[gnu/linux]]></category>
		<category><![CDATA[psp]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://blog.rom1v.com/2008/10/pspxconv%c2%a0-script-dencodage-de-videos-pour-psp/</guid>
		<description><![CDATA[J&#8217;ai récemment acheté une PSP, et je voulais pouvoir encoder mes vidéos facilement, avec les réglages que je voulais. J&#8217;ai donc écrit un script. Il s&#8217;utilise comme ceci : pspxconv fichier.avi fichier.mp4 500 si l&#8217;on veut convertir un .avi en .mp4 lisible par la PSP avec un bitrate vidéo de 500Kbps (pour l&#8217;instant le bitrate [...]]]></description>
			<content:encoded><![CDATA[<p>J&#8217;ai récemment acheté une PSP, et je voulais pouvoir encoder mes vidéos facilement, avec les réglages que je voulais. J&#8217;ai donc écrit un script.</p>
<p><a href="http://blog.rom1v.com/wp-content/uploads/2008/10/psp.png"><img src="http://blog.rom1v.com/wp-content/uploads/2008/10/psp-300x180.png" alt="psp" title="psp" width="300" height="180" class="aligncenter size-medium wp-image-227" /></a></p>
<p>Il s&#8217;utilise comme ceci :</p>
<pre>pspxconv fichier.avi fichier.mp4 500</pre>
<p>si l&#8217;on veut convertir un <code>.avi</code> en <code>.mp4</code> lisible par la PSP avec un bitrate vidéo de 500Kbps (pour l&#8217;instant le bitrate audio est fixé à 96).</p>
<p>Il est également possible de définir une qualité constante plutôt qu&#8217;un bitrate moyen (préférable pour <em>-hurry</em> et <em>-afap</em>) :</p>
<pre>pspxconv fichier.avi fichier.mp4 q20 -hurry</pre>
<p>Plusieurs <em>presets</em> de qualité sont disponibles :</p>
<ul>
<li><code>-afap</code> : as fast as possible (le plus rapidement possible) ; 1 seule pass en <strong>mpeg4</strong>, en 320×240, j&#8217;obtiens 190fps ;</li>
<li><code>-hurry</code> : 1 seule passe, en <strong>x264</strong>, en 480×272, meilleure qualité, j&#8217;ai environ 95fps ;</li>
<li><code>-std</code> (standard) : 2 pass, qualité normale (réglage à privilégier si on n&#8217;a pas de contraintes particulières), environ 55fps ;</li>
<li><code>-hq</code> : 2 pass, qualité un peu supérieure, environ 38 fps ;</li>
<li><code>-vhq</code> : extra haute qualité (inutile), environ 23 fps.</li>
</ul>
<p>On peut aussi rajouter des options de mencoder :<br />
<code>pspxconv fichier.avi fichier.mp4 500 -std -ss 10 -endpos 100</code><br />
n&#8217;encodera que de 10s à 110s.</p>
<pre>pspxconv fichier.avi fichier.mp4 500 -std -audiofile fichier.mp3</pre>
<p>encodera la vidéo <code>.avi</code> en utilisant la bande son <code>.mp3</code>.</p>
<p>Ce qu&#8217;il reste à améliorer (votre aide est la bienvenue) :</p>
<ul>
<li>possibilité d&#8217;utiliser plusieurs processeurs à 100% en <code>-afap</code> (même si on met plusieurs threads, 1 seul est à 100%)</li>
<li>possibilité de choisir à partir d&#8217;un <code>.mkv</code> la piste audio et la piste de sous-titres à utiliser et incruster.</li>
</ul>
<p>Voici le script :</p>
<pre>#!/bin/sh
# pspxconv : video encoding script for PSP
#
# 28th october 2008 - Romain Vimont (®om)
#
# v0.4 (25th june 2009)
#
# Converts input video and audio to mp4 { x264|mpeg4 + faac }, accepted by PSP.
#
# Syntax:
#   pspxconv input_file output_mp4 video_bitrate [quality_preset]
#     [mencoder_options [...]]
#
# video_bitrate can be an integer, or can define a quantifier if it starts
#   with q : q19.5 for example.
#
# quality_preset must be one of :
#   -afap : as fast as possible (poor quality mpeg4, 1 pass, fastest)
#   -hurry : (default) quite fast (poor quality, x264, 1 pass, fast)
#   -std :  standard quality (good quality, normal)
#   -hq  : high quality (very good quality, slow)
#   -vhq : very high quality (best quality, very slow)
#
# mencoder_options are appended to command line of mencoder call.
# For example :
#   -ss 10 -endpos 56 : trim the video from 10s to 66s.
#
# (of course, there are no options -afap, -hurry, -std, -hq nor -vhq)
#

# Syntax error detected.
# Exits the program with return code 1.
syntax_error() {
    printf '%s%s\n' "Syntaxe : $0 input_file output_mp4 video_bitrate " \
      '[quality_preset] [mencoder_options [...]]' >&#038;2
    exit 1
}

# Indicates whether the argument represents an integer.
#
# $1: value to test
# return: the integer if the argument represents an integer, an empty string if
#   it doesn't
is_integer() {
    local value="$1"
    printf %s "$1" | grep -o "^[[:digit:]]\+$"
}

# Returns the quantification value if the bitrate represents such a value.
#
# $1: value to test
# return: the value if the argument represents a quantifier, an empty string if
#   it doesn't
get_q_value() {
    local value="$1"
    printf %s "$1" | grep -o "^q[[:digit:]]\+\(\.[[:digit:]]\+\)\?$" | cut -c2-
}

# Gets a unique id, based on the clock (seconds + nanoseconds).
#
# return: unique id
uid() {
    date +'%s%N'
}

# Indicates whether the encoder must use x264 codec.
#
# $1: preset
# return: 'yes' if it must use it, nothing otherwise
use_x264() {
    local preset="$1"
    if [ "$preset" != '-afap' ]
    then
        printf 'yes'
    fi
}

# Returns the x264opts command line arguments for the selected preset.
#
# $1: preset
# return: command line arguments
x264_preset() {
    local preset="$1"
    local common='global_header:threads=auto'
#vbv_maxrate=1536:vbv_bufsize=2000:level_idc=30
    local common_q="$common:bframes=1:b_adapt:b_pyramid:weight_b:trellis=2"
    local q_value=$(get_q_value "$q")
    if [ "$q_value" ]
    then
        printf "crf=$q_value:"
    else
        printf "bitrate=$q:"
    fi
    case "$preset" in
    '-hurry') printf "$common:me=dia:subq=1" ;;
    '-std')  printf "$common_q" ;;
    '-hq')   printf "$common_q:me=umh:subq=6" ;;
    '-vhq')  printf "$common_q:me=esa:subq=7" ;;
    esac
}

# Returns the lavcopts command line arguments for the selected preset.
#
# return: command line arguments
mpeg4_preset() {
    local q_value=$(get_q_value "$q" | sed 's/\..*$//')
    if [ "$q_value" ]
    then
        printf "vqscale=$q_value:"
    else
        printf "vbitrate=$q:"
    fi
    printf 'aglobal=1:vglobal=1'
}

# Indicates whether the preset needs 2 passes.
#
# $1: preset
# return: '2' if the selected preset have 2 passes, nothing otherwise
two_passes_preset() {
    local preset="$1"
    if [ "$preset" != '-afap' -a "$preset" != '-hurry' ]
    then
        printf 2
    fi
}

# Indicates the scale selected for the preset.
#
# $1: preset
# return: width:height scaling
scale_preset() {
    local preset="$1"
    case "$preset" in
    '-afap')  printf 'dsize=320:240:0:16,scale=0:0' ;;
    *)        printf 'dsize=480:272:0:16,scale=0:0'
    esac
}

# error when less than 3 arguments
[ $# -ge 3 ] || syntax_error

# reads the arguments
in="$1"; shift
out="$1"; shift
q="$1"; shift

# audio bitrate
ab=96

# input file must exist
if [ ! -f "$in" ]
then
    printf '%s\n' "Input file doesn't exist : $in" >&#038;2
    exit 2
fi

# bitrate value must be integer or quantifier
if [ ! "$(is_integer "$q")" -a ! "$(get_q_value "$q")" ]
then
    printf '%s\n' "Bitrate value must be integer : $bitrate" >&#038;2
    exit 3
fi

# gets a unique filename, in order to avoid collisions when encoding two video
# at the same time
uid=$(uid)
tmp_log=$(printf '%s' "/tmp/$uid.log")

# chooses the quality preset
quality='-hurry'
if [ "$1" = '-afap' -o "$1" = '-hurry' -o "$1" = '-std' -o "$1" = '-hq' \
  -o "$1" = '-vhq' ]
then
    quality="$1"
    shift
fi

vf=$(scale_preset "$quality")

if [ $(use_x264 "$quality") ]
then
    # gets the x264 options
    opts=$(x264_preset $quality)
    if [ $(two_passes_preset "$quality") ]
    then
        # encodes the first pass
        mencoder "$in" -o /dev/null -of lavf -lavfopts format=mp4 -passlogfile \
        "$tmp_log" -ovc x264 -x264encopts "$opts:pass=1" \
        -vf "$vf" -nosound $@ &#038;&#038;

        # encodes the second pass
        mencoder "$in" -o "$out" -of lavf -lavfopts format=mp4 -passlogfile \
        "$tmp_log" -ovc x264 -x264encopts "$opts:pass=2" \
        -vf "$vf" -oac faac \
        -faacopts br=$ab:raw=yes:object=2 $@ &#038;&#038;

        exit 0
    else
        # encodes the unique pass
        mencoder "$in" -o "$out" -of lavf -lavfopts format=mp4 -passlogfile \
        "$tmp_log" -ovc x264 -x264encopts "$opts" \
        -vf "$vf" -oac faac \
        -faacopts br=$ab:raw=yes:object=2 $@ &#038;&#038;

        exit 0
    fi
else
    # encodes the unique pass
    mencoder "$in" -o "$out" -of lavf -lavfopts format=mp4 -passlogfile \
    "$tmp_log" -ovc lavc -lavcopts $(mpeg4_preset) -vf "$vf" -oac faac \
    -faacopts br=$ab:raw=yes:object=2 $@ &#038;&#038;

    exit 0
fi</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.rom1v.com/2008/10/pspxconv-script-dencodage-de-videos-pour-psp/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
