Page 1 of 2

Selecting different audio streams (languages)

PostPosted: Fri Jul 13, 2012 7:44 pm
by soundcheck
Hi folks.

Old issue still exists.

On my Bravia KDL I'm trying to run m2ts files which come with 2 audio streams lang1/lang2.

serviio always provides the first index stream. Is there a way that I can select the 2nd stream.
I do not know if serviio supplies 2 streams to the TV. But I guess so. Obviously it would be the TVs task to offer two languages/streams. It doesn't.

Now I'm wondering if I could introduce a rule which filters one audio stream resp one language on serviio level , so that the TV just gets one stream offered.

How can that be done. Any ideas are apprectiacted

Thx
SC

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 1:02 pm
by patters
It's still an open ticket at the moment. On the face of it, it seems like a simple feature to add. However the rule matching would actually be fairly complex. I've just been thinking about it and I propose breaking it down into device profile and console settings like so:

Serviio-Console
  • audio language preference order (comma separated, so "ENG,GER"), including the ability to set a particular language as the lowest priority. If you like watching foreign films with subtitles, you would want your own language *not* to be selected by default, with all other possible languages taking precedence (*,ENG). I think it should just be a text field in the GUI like in PS3MS, which would allow selection of streams which are using unofficial language abbreviation tags, which sometimes happens. Director commentary tracks can mean there are multiple tracks in the same lanuage, so...
  • number of audio channels preference (Prefer more channels/Prefer fewer channels).
A rule priority order would need to be defined (maybe move them up/down in the Console GUI - see below)

Device-specific (store in profiles.xml)
  • audio codec preference order (dts,ac3,aac,mp3). This value could be displayed by right-clicking on the renderer in Serviio-Console so the user can make an informed choice when deciding the rule priority order. Some users might want to edit this but they're probably power users who aren't afraid to edit the xml. Novice users would most likely be satisfied with the defaults, which are predominantly dictated by which audio formats the renderer device supports. By default we would want the "best" formats each device supports up front.
  • does the renderer support multiple audio streams? If so they could all be 'transwrapped' together, with the user selecting their preferred stream on the renderer device itself. If a renderer supports this feature, this should be enabled by default - ignoring all other rules.
  • if the renderer doesn't support multiple audio streams, do we want files that the renderer can play natively to be remuxed when they contain multiple audio (to allow the selection policy to take effect)?
  • If so which container do we want them remuxed to (mpegts is likely)?

Here's a UI mockup I put together in mspaint to visualise what I'm suggesting:

Image

A tooltip could explain that the language values are comma separated and that wildcards can be used. The policy matcher would run through the rules in order, stopping as soon as a single stream is matched.

Everyone is likely to want subtly different audio preferences, so having a flexible system will be key. I think having the device's capabilities read from profiles.xml, but then allowing this to be overridden by choosing the order in which the rules are applied fits this objective.

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 1:36 pm
by soundcheck
Reading all that sounds to me like an idea that might gets implemented in a year from now - if ever.

For now it would be nice to know how to make ffmpeg swap/or select the channels by language or channel-id via the profile file.

I could ssh into the server and switch a setup by script.

That would be at least something to begin with.

Edit: Just sneaked around ffmpeg:

Somehow I need to identify the relevant stream with ffmpeg -i and then use the -map to map it. Could e.g. that "ffmpeg -map" option be used in the profile file?

I also just learned that usually ffmpeg decodes the first audio stream it finds.

Cheers

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 1:42 pm
by patters
It will get added at some point - it's one of the most asked for features apart from DVD ISO support. Zip had been concentrating on the online functionality as a priority.

Well you could make your own wrapper for FFmpeg. I have one written for Linux. But you have no way of knowing whether the media file that's being selected for playback actually has more than one channel though. The alternative is that you wait for the feature and manually remux the videos you want to watch with tsMuxeR before you add them to the library. I've done that for a few of mine, and kept the original files, deleting the remuxed ones once I'd watched them.

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 1:50 pm
by soundcheck
That you can find out with ffmpeg -i while starting the wrapper ?!? Or I'd do it manually beforehand.

The challenge will be to select the right stream.

Could you post how you've written your wrapper? That would make life much easier. ;)


In my case I'd have to add the -map option.

Cheers

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 2:01 pm
by patters
True but I wonder whether the renderer will timeout if it doesn't get data pretty quickly. The additional ffmpeg -i might be too slow. Anyway, here's the wrapper I wrote to change the ac3 encoder on ARM CPU for Serviio 0.6.2. It was a real sod to write, even though I was only basing it on one someone had already written for the bash shell (this one was for the ash shell). The quote handling by the shell is the reason it's a pain.
  Code:
#!/bin/sh

#FFmpeg wrapper script to use non-floating point math AC-3 encoder on ARM CPUs
#as discussed here http://forum.serviio.org/viewtopic.php?f=5&t=5806

PARAMS=""
INPUT=0
for PARAM in "$@"; do
  if [ ${INPUT} = 1 ]; then
    #the FFmpeg input filename/URL needs quotes adding back on
    #because it may contain spaces, and the shell has removed them
    PARAMS="${PARAMS} \"${PARAM}\""   
    INPUT=0
  else
    PARAMS="${PARAMS} ${PARAM}"
  fi
  if [ "${PARAM}" == "-i" ]; then
    #this loop is the -i parameter, the next loop will be the input filename/URL
    INPUT=1
  fi
done

#make AC-3 encoder substitution
PARAMS="`echo ${PARAMS} | sed -e "s| -acodec ac3| -acodec ac3_fixed|"`"

#invoke FFmpeg
FOLDER="`dirname $0`"
echo "${FOLDER}/ffmpeg ${PARAMS}" > ${FOLDER}/../log/ffmpeg-wrapper.log
#need to use eval here otherwise the quotes aren't handled properly
#http://fvue.nl/wiki/Bash:_Why_use_eval_with_variable_expansion%3F
eval ${FOLDER}/ffmpeg ${PARAMS}

#return FFmpeg status
exit $?


You need to define your wrapper as the ffmpeg location parameter when starting Serviio:
http://www.serviio.org/index.php?option ... icle&id=43

Re: Selecting different audio streams (languages)

PostPosted: Sat Jul 14, 2012 2:35 pm
by soundcheck
Thx a lot. :D

I'll look into it.

Re: Selecting different audio streams (languages)

PostPosted: Sun Jul 15, 2012 7:23 am
by soundcheck
Here's my first shot wrapper:

Serviio selects the first audio stream (0:1) only by default. Serviio maps it already. So the "-map" option is already used (at least on my Bravia profile with .m2ts).
Below script just selects the 2nd audio stream (0:2) instead of the first one.

I moved /usr/local/bin/ffmpeg to /usr/local/bin/ffmpeg.bin and put below script there as "ffmpeg" executable. No need to change environment variables.

  Code:
#!/bin/sh
PARAMS="$@"
BIN="$(which ffmpeg.bin)"

#########################################################
# audio stream index as shown in ffmpeg -i, serviio default is 1
STREAM=2
#########################################################


PARAMS="$(echo "${PARAMS}" | sed "s/map 0:1/map 0:${STREAM}/")"
eval ${BIN} ${PARAMS}
exit $?


How to continue:

@ Patters: Obviously your earlier made proposal suggests to go via language priorities via console, which is not a bad idea at all.

It would be nice to know how to figure out the language of a stream (to improve the wrapper for the time being).


Cheers

Re: Selecting different audio streams (languages)

PostPosted: Sun Jul 15, 2012 8:20 am
by patters
Looking at your wrapper, I think it will break if the file path or filename contains a space (that was why my wrapper had that strange loop).

Re: Selecting different audio streams (languages)

PostPosted: Sun Jul 15, 2012 8:47 am
by soundcheck
patters wrote:Looking at your wrapper, I think it will break if the file path or filename contains a space (that was why my wrapper had that strange loop).


"$@" contains quite some spaces too !?!?

Re: Selecting different audio streams (languages)

PostPosted: Sun Jul 15, 2012 10:52 am
by patters
Not really. Each parameter is interpreted as a separate token by the shell at that point (even the ones containing spaces), as if each parameter was quoted. However, when you're putting them back into the string of parameters you'll use for ffmpeg the filename will be dropped in to that string with the spaces (it won't escape them) but without surrounding quotes. FFmpeg will later interpret these as separate nonsensical arguments and fail.

Re: Selecting different audio streams (languages)

PostPosted: Mon Jul 16, 2012 1:18 pm
by soundcheck
Hi Patters!

Just a quick idea:

Does this solve your escape problem?

PARAMS="$(echo ${PARAMS// /\\ })"


Cheers

Re: Selecting different audio streams (languages)

PostPosted: Mon Jul 16, 2012 10:54 pm
by patters
The script that I already posted solves the problem though.

Re: Selecting different audio streams (languages)

PostPosted: Tue Jul 17, 2012 1:43 pm
by zip
@patters: thanks for the suggestions, sounds reasonable to me. I'll try to plan this for the version after 1.0 (probably 1.1)

Re: Selecting different audio streams (languages)

PostPosted: Tue Jul 17, 2012 8:29 pm
by patters
Great!

Re: Selecting different audio streams (languages)

PostPosted: Sat Dec 01, 2012 3:21 pm
by Koczeka
Hi!

I do not even have the language setting part on the transcoding tab like on the printscreen in the second post. :cry:
I have installed the latest 1.01 version over 1.0 and still the same.
Help me if U can please. I want to set Hungarian as default.

THANKS,
Zoltan

Re: Selecting different audio streams (languages)

PostPosted: Mon Dec 03, 2012 6:43 am
by soundcheck
Zoltan.

Unfortunately this feature is still not implemented.

You need to apply the discussed workaround (ffmpeg-wrapper).


Cheers

Re: Selecting different audio streams (languages)

PostPosted: Sun Jan 13, 2013 10:36 am
by Koczeka
Can U guys please describe how to do this ffmpeg-wrapper workaround step by step? THANKS!

Re: Selecting different audio streams (languages)

PostPosted: Sun Jan 13, 2013 12:06 pm
by soundcheck
How about that:

  Code:
#!/bin/sh
###################################################################################################
# serviio ffmpeg wrapper script to assign different audio channels on-the-fly
# below STREAM variable need to be changed to e.g. 2 to select  2nd audio stream
# to be done prior to playback!
##################################################################################################
STREAM=1  # index 0 is video stream, 1 is 1st audio stream = serviio default  2= 2nd audio stream
###################################################################################################

PARAMS="$@"
BIN="$(which ffmpeg.bin)"


PARAMS="$(echo "${PARAMS}" | sed "s/map 0:1/map 0:${STREAM}/")"
eval ${BIN} ${PARAMS}
exit $?


1. Open terminal and become root
sudo su
2. Find your ffmpeg binary path
which ffmpeg
3. mv that binary
mv /<path>/ffmpeg /<path>/ffmpeg.bin
4. copy above ffmpeg-wrapper script
cp <ffmpeg-wrapper> /<path>/ffmpeg
5. change permissions
chmod 777 /<path>/ffmpeg /<path>/ffmpeg.bin

Make sure the wrapper executes /<path>/ffmpeg.bin inside the script as shown in above example.
If you update ffmpeg make sure you have a copy of the wrapper. You'll have to rerun above copy procedure.
Make sure there are not more then one ffmpeg binaries on the system ( e.g. /usr/bin and /usr/local/bin)

Good luck

Re: Selecting different audio streams (languages)

PostPosted: Wed Jan 30, 2013 3:52 am
by johnnyquid
For a ffmpeg wrapper solution for Windows, see post viewtopic.php?f=7&t=8481