> For the complete documentation index, see [llms.txt](https://docs.yemreak.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yemreak.com/arsiv/programming/extract-audio-from-video-with-typescript-and-ffmpeg.md).

# Extract audio from video with Typescript and ffmpeg

video, audio, programming, typescript, ffmpeg, code

![](https://i.imgur.com/z0OFsjk.png)

To extract audio from a video file in a TypeScript project, you can leverage the `fluent-ffmpeg` library, which is a wrapper around the FFmpeg command-line tool. FFmpeg must be installed on your system for `fluent-ffmpeg` to work. Here's how you can accomplish this task:

### Step 1: Install FFmpeg

Ensure FFmpeg is installed on your system. You can download it from [FFmpeg's official website](https://ffmpeg.org/download.html) or install it via a package manager if you're using Linux or macOS.

### Step 2: Install `fluent-ffmpeg`

If you haven't already, add `fluent-ffmpeg` to your project by running:

```shell
npm install fluent-ffmpeg
```

### Step 3: TypeScript Code to Extract Audio

Here's a simple TypeScript script that uses `fluent-ffmpeg` to extract audio from a video file and save it as an MP3 file. Ensure you have a `tsconfig.json` configured with `ESNext`, `CommonJS`, and strict typing.

```typescript
import ffmpeg from 'fluent-ffmpeg';

/**
 * Extracts audio from a video file and saves it as an MP3 file.
 * @param videoFilePath - The path to the video file.
 * @param outputAudioPath - The path where the extracted audio file should be saved.
 */
function extractAudioFromVideo(videoFilePath: string, outputAudioPath: string): Promise<void> {
    return new Promise((resolve, reject) => {
        ffmpeg(videoFilePath)
            .outputFormat('mp3') // Set the output format to mp3
            .on('end', () => {
                console.log(`Extraction completed: ${outputAudioPath}`);
                resolve();
            })
            .on('error', (err) => {
                console.error(`Error extracting audio: ${err.message}`);
                reject(err);
            })
            .save(outputAudioPath); // Specify the output file path
    });
}

// Example usage
const videoFilePath = 'path/to/your/video.mp4';
const outputAudioPath = 'path/to/output/audio.mp3';

extractAudioFromVideo(videoFilePath, outputAudioPath)
    .then(() => console.log('Audio extraction successful.'))
    .catch((err) => console.error(err));

```

**Important Notes:**

* **FFmpeg Dependency:** This script assumes FFmpeg is correctly installed and accessible in your system's PATH. If you encounter any issues, verify your FFmpeg installation.
* **Type Definitions:** If TypeScript cannot find the type definitions for `fluent-ffmpeg`, you may need to install them using `npm install @types/fluent-ffmpeg --save-dev`, assuming they are available, or use a `// @ts-ignore` directive above the import if necessary.
* **Error Handling:** The script uses Promises for asynchronous execution and error handling. Customize the error handling logic based on your application's requirements.

By following these steps, you should be able to extract audio from video files within your TypeScript project efficiently and effectively.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.yemreak.com/arsiv/programming/extract-audio-from-video-with-typescript-and-ffmpeg.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
