Skip to main content

Posts

Showing posts from January, 2022

Modifying Video Data using FFMPEG

FFMPEG has become very popular for so long time as a tool that can help us manipulate, convert, or configure video and audio data. We can resize our video, convert a file into a different container, change the bitrate, embed subtitles, or even stream real-time video. These are a few examples of its utilization. Converting file into a different container For example, we want to convert an MKV file into MP4 without changing its codec. ffmpeg -i input.mkv -codec copy output.mp4 Or, we want to convert an MP4 file into Webm with some customizations. ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 28 -b:v 0 -b:a 128k -c:a libopus output.webm Change video size We can resize a video width and height too. ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4 We can set the width or height to "-1" to keep the origin ratio. Note that some codecs require a size that can be divided by 2 to be successful. ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4 Insert subtitles We can attac...

Utilizing Decorators in Typescript

Decorator is a quite good feature in Typescript even though it is currently still in an experimental state. But, because of its advantages, it is commonly used by any developer. Such a framework like Angular also heavily uses this feature from the beginning. A decorator is a syntax for calling a function on the thing that it is decorated . It acts like a wrapper function or higher-order function that results in an improved thing or thing with additional things. The thing may be a class , class method , class property , or method property . The thing that will be decorated must have a specific type signature . A class decorator can only decorate a class, an object that has a constructor. @mydecorator class MyClass {} The code above is similar to the following code. class MyClass {} const WrappedClass = mydecorator(MyClass); We will take the example of utilizing a class decorator. First, we need to create a type of class constructor that passes a generic type for defining the ...

Fibonacci In Javascript Using Generator Function and Iterable

Generating the Fibonacci sequence is usually demonstrated in computer science courses to show an implementation of a recursive function. Besides the recursive function, we can utilize the generator function in Javascript for generating the sequence. Let us take a look at a few methods that we can perform. For instance, we will create some functions to get the value of a certain position in the Fibonacci sequence. Then, we will measure up the performance to get solid ground on what we should choose in our application. Recursive Function This is the simplest method for getting the value in the sequence. We use 1, not 0, as starting position to make this function more human. function getFibonacci1(pos) { if (pos === 1) { return 0; } if (pos === 2) { return 1; } return getFibonacci1(pos - 1) + getFibonacci1(pos - 2); } Generator Function In this method, we create a generator function for generating sequence and the actual function for providing the result. The ...

Schindler's List

It is one of my favorite films. This film is based on the true story of Oskar Schindler, a business owner in Germany during the second world war period. He is a smart man in the business, did bribe Nazi military officers, and employed Jews in his factory. His action gave hope for Jews while it cost him a lot and made him in a dangerous position. Even though it is only a small number of Jews that can be saved compared to millions who were killed, his action can become an inspiration for future generations. This film is very strong and has a wide view. We can feel how hard living in the war period as slaves is and how important kindness is.

Setting Up Next.js Project With ESLint, Typescript, and AirBnB Configuration

If we initiate a Next.js project using the  create-next-app tool, our project will be included with ESLint configuration that we can apply using yarn run lint . By default, the tool installs eslint-config-next and extends next/core-web-vitals in the ESLint configuration. The Next.js configuration has been integrated with linting rules for React and several other libraries and tools. yarn create next-app --typescript For additional configuration such as AirBnB, it is also possible. First, we need to install the peer dependencies of eslint-config-airbnb . We also add support for Typescript using eslint-config-airbnb-typescript . yarn add --dev eslint-config-airbnb eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks yarn add --dev eslint-config-airbnb-typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser After that, we can update the .eslintrc.json file for the new configuration. { "extends": [ "airb...

Storing Request Statistics Using Redis

If we build a backend service, we probably need to keep any statistics related to our service such as number of requests, average time for processing, number of errors, etc. We may also need to have statistic records in several time precisions for example hourly and daily. The simplest solution, we can just store any information into a table in our database, then calculate the summary when needed or by a request. But, it will cost our system storage and computing resources. Unlike relational databases or NoSQL that only have CRUD operations in general, Redis operations are related to the type of data that is stored. For example, a list will have push or pop operations, a sorted set will have a ranking, incrementing, or union operation, and so on. For instance for storing request statistics in Redis, we will store statistics of the response time of a backend application. The metrics are minimum time , maximum time , number of responses , and total response time . The last two metrics ...

Creating Connection in TypeORM

TypeORM is a complete ORM library for Node.js and other Javascript-based platforms which has support for a variety of databases such as PostgreSQL, MySQL, MongoDB, and so on. It can be utilized using Typescript or Javascript. There are several methods that can be used for creating connections in TypeORM. Create single connection await createConnection({ name, type, host, port, username, password, database }); Then, we can take the connection everywhere using getConnection() . const conn: Conection = getConnection(); Not only the connection, but we can also directly utilize EntityManager and Repository of the connection everywhere. const users = getManager().find(User); const user = getRepository(User).findOne(id); Another method is using getConnectionManager() . const connManager = getConnectionManager(); const conn = connManager.create({ ...params }); await conn.connect(); Create multiple connections await createConnections([{ ...params1 }, { ...params2 }]); Then, we ...