Search code examples
pythonpython-3.xvideoarabicmoviepy

Moviepy can't add arabic subtitles python


I am having an issue where i am unable to add arabic subtitles to an mp4 video. My old laptop running Ubuntu 20.04 can export video with arabic subtitles just fine. But on a new PC running Ubuntu 20.04 i cant't seem to do the same thing.

Please have a look of the code below.

generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')

subtitles = SubtitlesClip(final_subs, generator)

video = VideoFileClip("english.mp4")
result = CompositeVideoClip([video, subtitles.set_pos(('center','bottom'))])

result.write_videofile("output_english.mp4", fps=video.fps, temp_audiofile="temp-audio.m4a", codec="libx264", audio_codec="aac", threads=12, verbose=False)

Where this is the content of final_subs example

The exported video has contains only the sentence punctuation, and not the Arabic text. I have tried other languages and they work fine, this is only the case with Arabic

The image below shows the punctuation subtitles without text. example


Solution

  • After investigating for a long time, I found that the new system doesnt have arabic fonts installed.

    I have used the following command to install Microsoft arabic fonts.

    sudo apt install ttf-mscorefonts-installer
    sudo fc-cache -f
    

    To verify installation run the following command. fc-match Arial

    The video was exported successfully with the Arabic subtitles, but needed to be reshaped because the arabic text was mirrored. I reshaped the text using arabic_reshaper.