How to speed up video encoding?

It is taking a long time to encode a video. How can I speed this up? When it starts the encoding process, it takes a long time to finish encoding. Can I change the number of threads?

  • 1311
  • More
Replies (22)
    • It took about 2 hours for a 15 minute 720p video. Where can I find the ffmpeg command to change the amount of threads that are used to encode the video? Even though I am not sure if that is what the issue is.

      • You can change path to your own build of ffmpeg in inc/header.inc.php file:

        define('BX_SYSTEM_FFMPEG', '/path/to/ffmpeg.osx.exe'); ///< path to ffmpeg binary
        • Yes we can change path but we will not see next update of UNA.

          Update will show error that files are modified.

          • Hello Publicapp!

            The inc/header,inc.php file isn't included to the upgrade packs. Because of this file is autogenerated during the installation. So UNA will not foresee this file as a "upgrade breaker".

            • Come now Alex T, you know bloody well that most people using UNA can't compile their own versions of FFMPEG.  I have done it myself and it wasn't exactly a fun process.  Even Boonex does not compile their own version of FFMPEG.  Let me ask you this Alex T, have you ever compile FFMPEG?  If you have, then why not go ahead and compile FFMPEG with PThread support and add it to UNA?  Or, if you know of a compiled FFMPEG with PThread support then tell us where it is; you don't need to change the path, just replace the current FFMPEG.  I looked for FFMPEG with PThread and could not find it so that is why I went through the trouble to try to compile my own.

              • Publicapp I think that header.inc.php is excluded from the checking as Leonid said already, anyway you can enable the following option to make update work:

                Studio > Settings > System > Site Settings > Force auto-update, even if some files were modified

                • > have you ever compile FFMPEG?

                  Yes

                  > why not go ahead and compile FFMPEG with PThread support and add it to UNA?

                  We provide basic version which works on most servers and can't overload the server by taking all CPU power. Only recently we used 64bit version of ffmpeg, before 32bit version was always used. 

                  I'm very greatful who make static builds available for free it save us a lot of time, so we are using this opportunity and can do other important things. 

                  To increase ffmpeg performance for the best results you need to compile own ffmpeg which is using your hardware capabilities for the best performance. 

                  • Thanks guys for talking about this subject. Every comment is useful.

                    • I have an ffmpeg with pthreads. Where is the location to add "-threads 0"?

                      • Additional params can be added to sys_transcoder_filters table, find the transcoder where you want to add it, for example 'bx_videos_video_webm', then edit this row by adding ffmpeg_options param to filter_params field, where serialised array is stored, for example for 'bx_videos_video_webm' it would be:

                        Before adding custom options:

                        a:2:{s:1:"h";s:3:"480";s:10:"force_type";s:4:"webm";}

                        After adding custom options:

                        a:3:{s:1:"h";s:3:"480";s:10:"force_type";s:4:"webm";s:14:"ffmpeg_options";a:1:{s:7:"threads";i:0;}}
                        • Ok thanks. I have a few more questions. 

                          1. How does the encoding process work? Do the videos get encoded one after another or do they get stacked up to encode multiple videos simultaneously?

                          2. The video options only encode into 480p and ouputs to .webm, correct? Is there a way to make it so the videos keep their original resolution? So if someone uploads a 4k video, the output will be in 4k, or 1080p would stay as 1080p.

                          3. does .webm have a benefit over .mp4?

                          4. So if my goal is to raise the max resolution to 2160p, output in .mp4 format, using 4 threads, is the code below correct? 

                          a:3:{s:1:"h";s:3:"2160";s:10:"force_type";s:4:"mp4";s:14:"ffmpeg_options";a:1:{s:7:"threads";i:4;}}

                          5. What happens to the video if I raise the output resolution to 2160p? Would 720p videos encode to output 720p correctly or would the video be enlarged to 2160p?

                          • 1. Videos are added to the queue and processed one by one. At the same time only one vide os encoded.

                            2. You can try to remove "h" param at all, it maybe it will work.

                            3. Some browsers supports webm only this is the reason it's encoded to webm

                            4. You have incorrectly encoded string, it will not work, also you need to rise bitrate as well, default bitrate is 512k, you can specify it as 'video_bitrate' option, for example:

                            a:3:{s:1:"h";s:4:"2160";s:10:"force_type";s:3:"mp4";s:14:"ffmpeg_options";a:2:{s:7:"threads";i:4;s:13:"video_bitrate";s:5:"2048k";}}

                            5. I think it shouldn't stretch the video to a bigger size if it's smaller, but it's better to try.

                            • When you say remove all "h" param, do you mean like this?

                              a:3:{s:4:"2160";s:10:"force_type";s:3:"mp4";s:14:"ffmpeg_options";a:2:{s:7:"threads";i:4;s:13:"video_bitrate";s:5:"2048k";}}

                              • No, like this:

                                a:2:{s:10:"force_type";s:3:"mp4";s:14:"ffmpeg_options";a:2:{s:7:"threads";i:4;s:13:"video_bitrate";s:5:"2048k";}}

                                Please never edit this string directly, you need to use php functions unserialize to convert it to an array then edit php array then run serialize to make serialized array string.

                                • how do I edit the filter params field? There are no edit boxes.

                                  • Seems you need to execute MySQL queries via the "SQL" button. The query should have a look like:

                                    UPDATE `sys_transcoder_filters` SET `filter_params`='new_filter_params' WHERE `transcoder_object`='bx_timeline_photo_view' LIMIT 1

                                    Instead of bx_timeline_photo_view you may use the necessary object name.

                                    NB - backup this table before any actions there!

                                    • I don't mind if it is more complicated, I just need to make sure that it works. So it would be exactly like this?

                                      UPDATE `sys_transcoder_filters` SET `filter_params`='a:3:{s:1:"h";s:3:"480";s:10:"force_type";s:3:"mp4";s:14:"ffmpeg_options";a:1:{s:7:"threads";i:0;}}' WHERE `transcoder_object`='bx_videos_video_webm' LIMIT 1

                                      and is bx_videos_video_webm the only one that I need to change?

                                      Here is the unserialized code that I used:

                                      array (

                                        'h' => '480',

                                        'force_type' => 'mp4',

                                        'ffmpeg_options' => 

                                        array (

                                          'threads' => 0,

                                        ),

                                      );

                                      • You need to update 2 filters:

                                        UPDATE `sys_transcoder_filters` 
                                          SET  `filter_params` =  'a:3:{s:1:"h";s:3:"480";s:10:"force_type";s:3:"mp4";s:14:"ffmpeg_options";a:1:{s:7:"threads";i:0;}}' 
                                          WHERE `transcoder_object` =  'bx_videos_video_mp4' AND  `filter` =  'Mp4';
                                        UPDATE `sys_transcoder_filters` 
                                          SET  `filter_params` =  'a:3:{s:1:"h";s:3:"480";s:10:"force_type";s:4:"webm";s:14:"ffmpeg_options";a:1:{s:7:"threads";i:0;}}' 
                                          WHERE `transcoder_object` =  'bx_videos_video_webm' AND  `filter` =  'Webm';
                                        
                                        
                                        • Yes! Encoding was almost instant. Thank you.

                                          I am wondering about the jot messenger app and the albums app. These two apps allow video uploads. Does the  "bx_videos_video_mp4" and "bx_videos_video_webm" take care of those videos too? If so, then the issue is solved.

                                          • There are videos in other modules which have own record in sys_transcoder_filters table, so it needs to be updated separately. You can have a look at all records where filter field equals to Webm and Mp4 and make similar change. 

                                            • Ok, so threads => 0 works good with mp4, but with webm the "0" doesn't work, so I changed it to threads => 7. I added cpu_used => 5 to the code for webm and it slightly speeds it up, but I am unable to add token-parts => 3, and 'deadline realtime'. Threads won't work correctly for webm without token-parts and I think that 'deadline realtime' is looking for a number after it but a numerical value can't be set for it. That is the real reason why I don't want to use webm, because it takes forever to transcode.

                                              source: webmproject (dot) org/docs/encoder-parameters/

                                              Here's what's happening to me: 'token-parts' causes webm to fail and 'deadline realtime' causes webm to fail too, even after trying them all separately.

                                              Here is my unserialized code:

                                              array (

                                                'h' => '480',

                                                'force_type' => 'webm',

                                                'ffmpeg_options' => 

                                                array (

                                                  'threads' => 7,

                                                  'cpu-used' => 5,

                                                  'token-parts' => 3,

                                                  'deadline realtime',

                                                ),

                                              );;

                                              • Thank you for you notices, we'll try to do something with it:

                                                https://github.com/unaio/una/issues/1852

                                                Login or Join to comment.