Sunday, November 25, 2018

Combining mp4 Files Without Re-encoding Using FFmpeg on Windows Subsystem for Linux

I recently used FFmpeg installed on the Windows Subsystem for Linux on my Windows 10 machine to combine some mp4 video files without re-encoding the files. Being able to do this without re-encoding makes the process much quicker and does not result in image degradation.

Install the Windows Subsystem for Linux

If it is not already installed, you will need to install the Windows Subsystem for Linux. Instructions for doing this can be found at https://docs.microsoft.com/en-us/windows/wsl/install-win10. I have the Ubuntu distro installed (https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6).

Install FFmpeg

To install FFmpeg (https://ffmpeg.org/) on Ubuntu open the Ubuntu command line and enter the following:
sudo apt install ffmpeg

Create Merge File

Using the text editor of your choice, create a text file that contains the list of mp4 files you want to combine. For this example I called the file input.txt. The format of this file is as follows:
file 'part1.mp4'
file 'part2.mp4'

Combine mp4 Files

The following command can be used to merge the files listed in the text file from the step above into a single mp4 output file without re-encoding the files:
ffmpeg -f concat -i input.txt -c copy output.mp4
The name of the new mp4 file in the example above is output.mp4.

Saturday, November 10, 2018

Trimming mp4 Files Without Re-encoding Using FFmpeg on Windows Subsystem for Linux

I recently used FFmpeg installed on the Windows Subsystem for Linux on my Windows 10 machine to trim the beginning and ending off of some mp4 video files without re-encoding the files. Being able to do this without re-encoding makes the process much quicker and does not result in image degradation.

Install the Windows Subsystem for Linux

If it is not already installed, you will need to install the Windows Subsystem for Linux. Instructions for doing this can be found at https://docs.microsoft.com/en-us/windows/wsl/install-win10. I have the Ubuntu distro installed (https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6).

Install FFmpeg

To install FFmpeg (https://ffmpeg.org/) on Ubuntu open the Ubuntu command line and enter the following:
sudo apt install ffmpeg

Trim the Beginning of a Video

The following command can be used to trim the first 30 seconds off the front of an mp4 file:
ffmpeg -ss 00:00:30 -i input.mp4 -c copy output.mp4

Trim the Ending of a Video

The following command can be used to trim an mp4 file at a specified position. In this example it copies the first thirty seconds of the input.mp4 file and copies it to the output.mp4 file.
ffmpeg -i input.mp4 -c copy -to 00:00:30 output.mp4
Another way to accomplish this is to specify a duration. In this example it copies the first fifteen seconds of the input.mp4 file and copies it to the output.mp4 file.
ffmpeg -i input.mp4 -c copy -t 00:00:15 output.mp4

Trim the Beginning and Ending of a Video

The following command can be used to trim the first 30 seconds off the front of an mp4 file and then copy the next three minutes and forty five seconds. The resulting file will be three minutes and forty five seconds long:
ffmpeg -ss 00:00:30 -i input.mp4 -c copy -t 00:03:45 output.mp4

Wednesday, December 20, 2017

Find a Penny, Pick It Up...

A coworker of mine was working on a TypeScript/JavaScript application that ran in a web browser. The application would display a dollar amount that was about to be charged to a customer's credit card. When the customer acknowledged the amount, the customer would be taken to a page hosted by the payment processor to actually perform the transaction. This seemed to be working correctly, but our QA tester found that certain dollar amounts, like $8.95, would show in our application as $8.95, but would be displayed as $8.94 on the payment processor's page.

The payment processors required monetary amounts to be sent in as cents, rather than dollars. For example, $54.12 would be sent as 5412. The developer had written something similar to the following to do this value conversion:

1
2
const amount: number = 8.95;
const paymentProcessorValue: number = amount * 100;

The problem ended up being caused by the way JavaScript does math. JavaScript uses floating-point arithmetic to do calculations. In this particular case, 8.95 * 100 was not returning the expected 895, but instead was returning 894.9999999999999. The payment processor truncated the value at the decimal point, therefore it interpreted this value as $8.94, not $8.95.

The issue was corrected by rounding the value before sending it on to the payment processor.

1
2
const amount: number = 8.95;
const paymentProcessorValue: number = Math.round(amount * 100);

Example