Adding subtitle to mp4 movie with ffmpeg

I need to add a short copyright message at the beginning and ending of a short movie. So, I think it will be great to use ffmpeg for this simple task. Well, it turned out to be not as simple as I initially expected.

Running the initial simple command (show below) taken from somewhere in WWW result in error in reading the .srt file.

ffmpeg -i input-dv.mp4 -vf subtitles=copyright.srt output-dv.mp4

Initially, I have my .srt setup as follows:

1
00:00:00,000-->00:00:10,000
The movie should only be used in ...

Since the error message said it “cannot read” the file, I tried to change its name, adding fullpath, etc. Nothing worked. I also tried to change the contain. And finally, the following works. See the difference? I missed the spaces sounding “–>”!

1
00:00:00,000 --> 00:00:10,000
The movie should only be used in ...

Now the program run and produced a result mp4 file. However, when I viewed the movie, there was no subtitle! After reviewing the screen full of messages displayed during the conversion, I found there was a warning message about the missing fonts.conf file. Another round of information digging began.

This time, luckily, not before long, I came across a post (https://forum.videohelp.com/threads/371053-ffmpeg-burnt-in-subtitles-Fontconfig-invalid-attribute-mode) where it provided most information about setting up the fonts.conf file.

Here are steps I took:

1. Download the fonts.conf from the above post and put it in the root directory of ffmpeg which, in my case, is D:\WinApps\ffmpeg

2. Setup the envoironment variables as follows:

set FC_CONFIG_DIR=d:\WinApp\ffmpeg
set FONTCONFIG_FILE=fonts.conf
set FONTCONFIG_PATH=d:\WinApp\ffmpeg

3. Run the following command

ffmpeg -i input-dv.mp4 -vf subtitles=copyright.srt:force_style='Fontsize=18' output-dv.mp4

The first time I run the command, it took a bit longer to process.

The final problem is adding Chinese in the subtitle text. This turned out to be the most simple one to be solved. Just make sure the file encoding is in UTF-8. For this, I use the feature provided notepad directly.

Advertisements

“int RECV_PIN = TKD2;” error in Arduino IR library

Ok, it has been a long time since my last play-around with Arduino. This time, I tried to build a simple IR controller to open and close a window shader that I am planing to build. So I copy a sample program and test it out and get the error “TKD2 not defined” error in the following line of code in the file IRremoteTools.cpp which is part of the IRremote library.

int RECV_PIN = TKD2; // the pin the IR receiver is connected to

The simplest solution for this problem turned out to be just delete the two files IRremoteTools.cpp and IRremoteTools.h from the library directory. These two files just provide some additional function interface to the library which I am quite happy to live without them.

Allowing a local WiFi router to connect to the Internet through a wire LAN

I recently need to set up a WiFi router in my room where I have a computer that connects to a wired network with 802.1x authentication. The network is connected to the internet. Since the WiFi router cannot directly connect to a network with 802.1x authentication, the router is connected to the computer (running Ubuntu 16.04.1 LTS) instead. The following is the code, allowing the traffic from the WiFi router through my computer connect to the network with Internet capability.

#
# Perform NAT so that the WiFi route can connect to the Internet
# eth0 connect to a network which can connect to the Internet
# eth1 connect to LAN through WiFi router
#
# code taken from http://www.revsys.com/writings/quicktips/nat.html
#
echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

“symbol(s) not found for architecture x86_64 error” when compiling OpenCV example program in Mac OS X 10.12

I just completed the installation of OpenCV 3 in my Mac (running OS X 10.12) after a long search of various installation methods.  However, when I tried to compile a sample C++ program, the following error was resulted:

Undefined symbols for architecture x86_64:
  "cv::imread(cv::String const&, int)", referenced from:
      _main in DisplayImage-6f69a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [DisplayImage] Error 1

So, once again, the search is on. After searching for the entire morning for solutions, I came across many “solution posts” that did not work!

Finally, I reached a post that mentioned “imread has been moved in the opencv_imgcodecs library.” and should “Try to add -lopencv_imgcodecs to your linker flags.” This solved my problem.

This case made me wonder how many people who posted “incorrect solution” had actually tried to replicate the error and actually solved the problem? Or did they just “think” what may be the problem and provide “guesses” about the possible solutions?

I am not saying that try ones best to provide information to the other is a bad thing. But providing too many “unproven” information in the web not only not able to help, it also makes the searching for true solution more difficult!

Using cmake-gui to Create MinGW lib for OpenCV 2.4.13

Recently, I have tried to learn how to use OpenCV. Of course, the first thing is to install OpenCV. The version I tried to install is 2.4.13. The development tools I used is MinGW g++. After download and install all the tools (including cmake), I soon found out that there is no library for MinGW g++ in the distribution. This means I need to build my own if I want to use g++. The search is on…

There is not much information I can find for my environment which is Windows 10 and using g++ as my main c++ compiler. After putting the pieces together, I eventually hit the right path, the following is my procedure for building the MinGW lib for OpenCV 2.4.13:

  1. run cmake-gui as follows:
    cmake-gui -G "MinGW Makefiles"

    Setup the location for source (in my case is D:/WinApps/opencv/sources) and the binaries (D:/WinApps/opencv/build/x86/mingw). Add the following two entries as well:

    CMAKE_CXX_COMPILER <em>path_to_g++</em>
    CMAKE_C_COMPILER <em>path_to_gcc</em>
  2. Click Configure then with the default selection of components click Generate
  3. In cmd, change directory to D:/WinApps/opencv/build/x86/mingw and execute the command
    make all

    I encountered two errors during the make process:

    • A compile error for the file window_w32.cpp where the symbols ‘BTNS_AUTOSIZE‘ and ‘BTNS_BUTTON‘ were not declared. I fixed it by change them to 16 and 0 (where I found in one of the header file of g++).
    • Another error from the files in ‘nonfree’ module, for which I just deselect the ‘BUILD_opencv_nonfree’ from cmake and reclick Generate again, then make all again. This time, it went all the way to finish without serious error.
  4. Done!

I test drive the system by creating a c++ program loadimg.cpp which I copied from here.

g++ -I"D:\WinApps\opencv\build\include" -L"D:\WinApps\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core2413 -lopencv_highgui2413 -o loadimg

The program compiled and run!

Error 1935 while try to upgrade blue tooth drive for lenovo X201

I recently tried to test my blue tooth stereo headset with my lenovo X201 and end up need to update the blue tooth driver in the computer. However, during the update process, the computer encountered an error 1935 and the update was not successful and rolled back. I tried a few times with the same result. I even tried to download and install the new driver manually, still without any luck. Reading the forums from web also gave me not much help. I finally do the following and magically it worked!

  1. In “Device Manager” (right-click Computer->property), right click the ThinkPad Bluetooth device and click “Uninstall”. The entire BT device will gone.
  2. In “Device Manager”, right click the top item (my notebook) and click “Scan for hardware changes”, wait a short while and it should find the BT device and start installing the most updated drivers!

Now, the device drivers are the most updated one!

Finding bit rate

I recently have a need to down-size of mp4 file in order to be able to upload it to a web page. The upload limit of the web page is 32M where the size of my mp4 file is about 200+M. I found out that I can use ffmpeg to re-compress the file with a smaller bit rate in order to down-size the file. The only problem is what bit rate to use. The command I used is as follows:

ffmpeg -i original.mp4 -b:v 126k -b:a 64k output.mp4

As you can see, I can control both the video bit rate and audio bit rate separately. After a few tests I eventually find our how to do the math. Given the length of my video is 22:49. The total bit rate is calculated as follows:

Total bit rate = (target file size / duration) * 8
               = (32 * 1024 * 1024) / (22 * 60 + 49) * 8
               = 196081
               = 191k

In other words, my maximum bit rate should be 191k, this is the combined total bit rate for both video and audio. Thus, I can select different combination of video and audio bit rate as long as they sum is within 191k. In the above example, I use 126k for video and 64k of audio, the result file is 33,414,347 bytes (of 31.9M) long. Good enough for me.