FFMpeg Error initializing filter 'drawtext'

2019-08-24 15:02发布

问题:

I try to draw a text on a video using FFMpeg but I have the error:

ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
      built with gcc 4.8 (GCC)
      configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
      libavutil      55. 17.103 / 55. 17.103
      libavcodec     57. 24.102 / 57. 24.102
      libavformat    57. 25.100 / 57. 25.100
      libavdevice    57.  0.101 / 57.  0.101
      libavfilter     6. 31.100 /  6. 31.100
      libswscale      4.  0.100 /  4.  0.100
      libswresample   2.  0.101 /  2.  0.101
      libpostproc    54.  0.100 / 54.  0.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/mediapicker/videos/eb89fc80-ebcc-4848-a95c-6983b2c5b09d.mp4':
      Metadata:
        major_brand     : 3gp4
        minor_version   : 0
        compatible_brands: isom3gp4
        creation_time   : 2018-10-03 15:46:13
        com.android.version: 6.0
      Duration: 00:00:04.78, start: 0.000000, bitrate: 6167 kb/s
        Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 6122 kb/s, SAR 1:1 DAR 16:9, 19.95 fps, 19.92 tbr, 90k tbn, 180k tbc (default)
        Metadata:
          rotate          : 90
          creation_time   : 2018-10-03 15:46:13
          handler_name    : VideoHandle
        Side data:
          displaymatrix: rotation of -90.00 degrees
        Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
        Metadata:
          creation_time   : 2018-10-03 15:46:13
          handler_name    : SoundHandle
    Fontconfig error: Cannot load default config file
    [Parsed_drawtext_0 @ 0xb561e040] impossible to init fontconfig
    [AVFilterGraph @ 0xb5609140] Error initializing filter 'drawtext' with args '"fontfile=/usr/share/fonts/TTF/Vera.ttf: text=Test Heading"'
    Error opening filters!

This is my code:

 private GLSurfaceView mGLView;
    private Button chooseVideo;
    private Button valideButton;
    private String SelectedVideo;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chooseVideo= findViewById(R.id.choose_video);
        chooseVideo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                new VideoPicker.Builder(MainActivity.this)
                        .mode(VideoPicker.Mode.GALLERY)
                        .directory(VideoPicker.Directory.DEFAULT)
                        .extension(VideoPicker.Extension.MP4)
                        .enableDebuggingMode(true)
                        .build();
            }
        });
        videoView = (VideoView) findViewById(R.id.videoView);
        final FFmpeg ffmpeg = FFmpeg.getInstance(MainActivity.this);
        try {
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onStart() {
                }

                @Override
                public void onFailure() {
                }

                @Override
                public void onSuccess() {
                }

                @Override
                public void onFinish() {
                }
            });
        } catch (FFmpegNotSupportedException e) {
        }
        valideButton= findViewById(R.id.valide);
        valideButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    String[] cmd = {"-y" , "-i", SelectedVideo,
                            "-vf", "drawtext=\"fontfile=/usr/share/fonts/TTF/Vera.ttf: text=\'Test Heading\'\"",
                            "-acodec",
                            "copy",                       Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_MOVIES+"/output.mp4"};

                    ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                        @Override
                        public void onStart() {
                        }

                        @Override
                        public void onProgress(String message) {
                        }

                        @Override
                        public void onFailure(String message) {
                            Log.i("ffmpegFailure",message);
                        }

                        @Override
                        public void onSuccess(String message) {
                            Log.i("ffmpegExecute",message);
                            videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_MOVIES+"/output.mp4");
                            videoView.requestFocus();
                            videoView.start();
                        }
                        @Override
                        public void onFinish() {
                        }
                    });
                } catch (FFmpegCommandAlreadyRunningException e) {
                    // Handle if FFmpeg is already running
                }
            }
        });
}

My problem is that I can not use the option 'drawtext'. When I don't use the option 'drawtext' it's work. I do not know which syntax I should use. I tried a lot of possibilities without success. I use this library: http://writingminds.github.io/ffmpeg-android-java/

Can you help me?

回答1:

First You need to specify the FONTCONFIG_FILE to be used in your command. FFMpeg depends on this file to locate fonts and render you text.

Your command will look something like this.

String[] cmd = {"-y" , "-i", SelectedVideo,
                            "-vf", "drawtext=\"FONTCONFIG_FILE=/path/to/configfile.conf:fontfile=/system/fonts/DroidSans-Bold.ttf:text=\'Test Heading\'\"",
                            "-acodec",
                            "copy", Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_MOVIES+"/output.mp4"};

You can find a sample configuration file for android and add it to your project and refer to it.

Here is a sample configuration file:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>

<!-- Font directory list -->

    <dir>/system/fonts</dir>
    <!-- add your font folders here -->

<!-- Font cache directory list -->

    <cachedir>.</cachedir>

<!--
  Accept deprecated 'mono' alias, replacing it with 'monospace'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>mono</string>
        </test>
        <edit name="family" mode="assign" binding="same">
            <string>monospace</string>
        </edit>
    </match>

<!--
  Accept alternate 'sans serif' spelling, replacing it with 'sans-serif'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>sans serif</string>
        </test>
        <edit name="family" mode="assign" binding="same">
            <string>sans-serif</string>
        </edit>
    </match>

<!--
  Accept deprecated 'sans' alias, replacing it with 'sans-serif'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>sans</string>
        </test>
        <edit name="family" mode="assign" binding="same">
            <string>sans-serif</string>
        </edit>
    </match>

    <config>
<!--
  These are the default Unicode chars that are expected to be blank
  in fonts.  All other blank chars are assumed to be broken and
  won't appear in the resulting charsets
 -->
        <blank>
            <int>0x0020</int>   <!-- SPACE -->
            <int>0x00A0</int>   <!-- NO-BREAK SPACE -->
            <int>0x00AD</int>   <!-- SOFT HYPHEN -->
            <int>0x034F</int>   <!-- COMBINING GRAPHEME JOINER -->
            <int>0x0600</int>   <!-- ARABIC NUMBER SIGN -->
            <int>0x0601</int>   <!-- ARABIC SIGN SANAH -->
            <int>0x0602</int>   <!-- ARABIC FOOTNOTE MARKER -->
            <int>0x0603</int>   <!-- ARABIC SIGN SAFHA -->
            <int>0x06DD</int>   <!-- ARABIC END OF AYAH -->
            <int>0x070F</int>   <!-- SYRIAC ABBREVIATION MARK -->
            <int>0x115F</int>   <!-- HANGUL CHOSEONG FILLER -->
            <int>0x1160</int>   <!-- HANGUL JUNGSEONG FILLER -->
            <int>0x1680</int>   <!-- OGHAM SPACE MARK -->
            <int>0x17B4</int>   <!-- KHMER VOWEL INHERENT AQ -->
            <int>0x17B5</int>   <!-- KHMER VOWEL INHERENT AA -->
            <int>0x180E</int>   <!-- MONGOLIAN VOWEL SEPARATOR -->
            <int>0x2000</int>   <!-- EN QUAD -->
            <int>0x2001</int>   <!-- EM QUAD -->
            <int>0x2002</int>   <!-- EN SPACE -->
            <int>0x2003</int>   <!-- EM SPACE -->
            <int>0x2004</int>   <!-- THREE-PER-EM SPACE -->
            <int>0x2005</int>   <!-- FOUR-PER-EM SPACE -->
            <int>0x2006</int>   <!-- SIX-PER-EM SPACE -->
            <int>0x2007</int>   <!-- FIGURE SPACE -->
            <int>0x2008</int>   <!-- PUNCTUATION SPACE -->
            <int>0x2009</int>   <!-- THIN SPACE -->
            <int>0x200A</int>   <!-- HAIR SPACE -->
            <int>0x200B</int>   <!-- ZERO WIDTH SPACE -->
            <int>0x200C</int>   <!-- ZERO WIDTH NON-JOINER -->
            <int>0x200D</int>   <!-- ZERO WIDTH JOINER -->
            <int>0x200E</int>   <!-- LEFT-TO-RIGHT MARK -->
            <int>0x200F</int>   <!-- RIGHT-TO-LEFT MARK -->
            <int>0x2028</int>   <!-- LINE SEPARATOR -->
            <int>0x2029</int>   <!-- PARAGRAPH SEPARATOR -->
            <int>0x202A</int>   <!-- LEFT-TO-RIGHT EMBEDDING -->
            <int>0x202B</int>   <!-- RIGHT-TO-LEFT EMBEDDING -->
            <int>0x202C</int>   <!-- POP DIRECTIONAL FORMATTING -->
            <int>0x202D</int>   <!-- LEFT-TO-RIGHT OVERRIDE -->
            <int>0x202E</int>   <!-- RIGHT-TO-LEFT OVERRIDE -->
            <int>0x202F</int>   <!-- NARROW NO-BREAK SPACE -->
            <int>0x205F</int>   <!-- MEDIUM MATHEMATICAL SPACE -->
            <int>0x2060</int>   <!-- WORD JOINER -->
            <int>0x2061</int>   <!-- FUNCTION APPLICATION -->
            <int>0x2062</int>   <!-- INVISIBLE TIMES -->
            <int>0x2063</int>   <!-- INVISIBLE SEPARATOR -->
            <int>0x206A</int>   <!-- INHIBIT SYMMETRIC SWAPPING -->
            <int>0x206B</int>   <!-- ACTIVATE SYMMETRIC SWAPPING -->
            <int>0x206C</int>   <!-- INHIBIT ARABIC FORM SHAPING -->
            <int>0x206D</int>   <!-- ACTIVATE ARABIC FORM SHAPING -->
            <int>0x206E</int>   <!-- NATIONAL DIGIT SHAPES -->
            <int>0x206F</int>   <!-- NOMINAL DIGIT SHAPES -->
            <int>0x2800</int>   <!-- BRAILLE PATTERN BLANK -->
            <int>0x3000</int>   <!-- IDEOGRAPHIC SPACE -->
            <int>0x3164</int>   <!-- HANGUL FILLER -->
            <int>0xFEFF</int>   <!-- ZERO WIDTH NO-BREAK SPACE -->
            <int>0xFFA0</int>   <!-- HALFWIDTH HANGUL FILLER -->
            <int>0xFFF9</int>   <!-- INTERLINEAR ANNOTATION ANCHOR -->
            <int>0xFFFA</int>   <!-- INTERLINEAR ANNOTATION SEPARATOR -->
            <int>0xFFFB</int>   <!-- INTERLINEAR ANNOTATION TERMINATOR -->
        </blank>
<!--
  Rescan configuration every 30 seconds when FcFontSetList is called
 -->
        <rescan>
            <int>30</int>
        </rescan>
    </config>

</fontconfig>

If you need to access fonts stored in another folder (other than system/fonts) you must add your folder to the configuration file.