SofdecTutorials

Multilingual movies with Sofdec

When localizing games in various languages, it is important for video playback to select the correct audio track and to display the right subtitles. In this post, we explain how to play movies in such multilingual environments. The CRI Mana library provides two features for multilingual use: subtitle acquisition and sub-audio playback.

Creating data containing subtitle text and audio

To encode a video for Sofdec, you can use the Sofdec2EncWiz.exe tool, which will create the final .usm encoded file based on your settings. In this workflow, you can register the multilingual audio data and the subtitle text in the windows shown below. Register the narration and text files for each language you want to play in the game.

Demo in Unity

Let’s take a look at an example where we can change the narration audio and subtitles of a usm video from a script in Unity. Open the following Unity scene from the official SDK:SDK\unity\samples\UnityProject\Assets\Scenes\crimana\basicScene_08_Multilingual.unity

In this scene, you can change the narration and subtitle channels with the buttons on the right side of the screen. The scene references only one video file, and the subtitle audio and subtitle text that are played and displayed are read from the script.

Main Script

The corresponding sample code can be seen below. Below is a part of Scene_08_Multilingual.cs from the official SDK.



using System.Collections;
using UnityEngine;
using CriWare;
using CriWare.CriMana;using CriMana;
using UnityEngine.UI;
public class Scene_08_Multilingual : MonoBehaviour
{
    [SerializeField]
    CriManaMovieMaterial manaMovieMaterial = null;
    [SerializeField]
    Text text = null;

    /* array of available sub-audio tracks */
    int[] subAudioTracks = { 16, 17, 18 };
    /* array of available sub-title tracks */
    int[] subtitleChannels = { 0,1 };

    int currentSubaudio, currentSubtitle;

    Player player;

    private void Awake()
    {
        player = manaMovieMaterial.player;
        currentSubaudio = subAudioTracks[0];
        currentSubtitle = subtitleChannels[0];
        player.SetSubAudioTrack(currentSubaudio);
        player.SetSubtitleChannel(currentSubtitle);
    }

    private void OnEnable()
    {
        /* add a listener to observe subtitle changes */
        player.OnSubtitleChanged += UpdateSubtitle;
    }

    private void OnDisable()
    {
        player.OnSubtitleChanged -= UpdateSubtitle;
    }

    /* method called from player.OnSubtitleChange event */
    void UpdateSubtitle(System.IntPtr ptr)
    {
        text.text = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptr, player.subtitleSize);
    }

    IEnumerator ChangeSubAudioTrack(int track)
    {
        /* call Stop and wait until the player stops */
        /* subaudio track can be changed when player-state is "Stop" */
        player.Stop();
        while (true)
        {
            if (player.status == Player.Status.Stop) break;
            yield return null;
        }
        /* set subaudio track and restart */
        player.SetSubAudioTrack(track);
        player.Start();
        currentSubaudio = track;
    }

    void ChangeSubtitleChannel(int channel)
    {
        /* change subtitle channel */
        manaMovieMaterial.player.SetSubtitleChannel(channel);
        currentSubtitle = channel;
    }

    /* Displaying GUI and prosessing user operations */
    void OnGUI()
	{
      /* -- truncated -- */
	}
}

Overall Operation Flow

  1. When the script is initialized, the default audio track and subtitle channel are set. Various buttons are drawn in OnGUI.
  2. The user selects the audio track or subtitle channel through the UI.
  3. When the audio track is changed, the player is paused, the new track is set, and playback is resumed.
  4. When a new subtitle channel is selected, the change is immediate.
  5. Subtitle updates are reflected in real-time in the UI via the OnSubtitleChanged event.

As you can see, with minimal coding, you can implement multilingual movies in your game. This is only one of the many benefits of using Sofdec to encode and play your videos!