Is there a way to import the pydub package into N8N?

Hey,
I’m using the Whisper API to transcribe some audio, but when the file is too big I want to use Pydub to split it up into 10 minute intervals. How possible is this? I’m not sure, so that’s why I need bigger brains than mine to come into the equation.

I know the Code function now supports Python, and I’m wondering if anyone knows if it’s possible to use the pydub package, and how to install it.

The code I’m trying to test run is the sample one from the OpenAI Whisper Docs:

from pydub import AudioSegment

song = AudioSegment.from_mp3("good_morning.mp3")

# PyDub handles time in milliseconds
ten_minutes = 10 * 60 * 1000

first_10_minutes = song[:ten_minutes]

first_10_minutes.export("good_morning_10.mp3", format="mp3")

and the error I’m getting in N8N is the following:

Error: ModuleNotFoundError: No module named 'pydub'

    at PythonSandbox.getPrettyError (/usr/local/lib/node_modules/n8n/packages/nodes-base/dist/nodes/Code/PythonSandbox.js:72:20)
    at PythonSandbox.runCodeInPython (/usr/local/lib/node_modules/n8n/packages/nodes-base/dist/nodes/Code/PythonSandbox.js:59:24)
    at PythonSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/packages/nodes-base/dist/nodes/Code/PythonSandbox.js:25:33)
    at Object.execute (/usr/local/lib/node_modules/n8n/packages/nodes-base/dist/nodes/Code/Code.node.js:116:26)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/packages/workflow/dist/Workflow.js:673:19)
    at /usr/local/lib/node_modules/n8n/packages/core/dist/WorkflowExecute.js:652:53

As of now, I’m just trying to get rid of the “No module named ‘pydub’” error, I’ll be filling in all my custom inputs afterward.

Thank you guys in advance.

Information on your n8n setup

  • n8n version: Version 1.15.1 - image: n8nio/n8n:ai-beta
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker via Digital Ocean
  • Operating system: Linux

Or if you have a solution using FFmpeg that would be ideal!

Hi @itsalanlee, n8n’s Python implementation only supports packages compatible with Pyodide as documented here I am afraid. pydub is unfortunately not on the list.

You could, however, use Python (or FFmpeg’s binaries) through the Execute Command node. Seeing you’re using docker you’d need to build a custom image for these tools to be available, but it’s not too much work.

Your Dockerfile could look like so (this will install python3, ffmpeg, curl and a few example Python packages using pip):

# Use the original n8n image as the basis
FROM n8nio/n8n:ai-beta

# Install additional packages as needed
USER root
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 curl ffmpeg
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools speedtest-cli 
USER node

Assuming you’re also using docker-compose, you’d also need to specify the build action in your docker-compose.yml file. If both Dockerfile and docker-compose.yml live in the same directory, the docker-compose.yml file itself can as simple as this:

services:
  n8n:
    build: .
    restart: unless-stopped
    ports:
      - 5678:5678
    volumes:
      - ./data:/home/node/.n8n

Finally, build n8n using docker pull n8nio/n8n:ai-beta && docker compose build and start it using docker compose up --detach.

Afterwards you can use ffmpeg or python (or any other package you might install of course) right through the Execute Command node in n8n:

Hope this helps!

2 Likes

Hey @MutedJam ,
Thank you so much for taking the time to write all of this out and helping me, I trully appreciate you!

I created the Dockerfile, it ran everything successfully, I then ran “docker compose down” and proceeded to run “docker compose up --detach”, however it’s now saying the site can’t be reached and says it’s refusing to connect.

To give you more context, I’m running my n8n with Digital Ocean, and I set it up with n8n caddy (n8n lives in a folder called n8n-docker-caddy in the root user)

I tried what I could to solve this but I’m in no luck. I’m not sure if one of the files (Dockerfile or docker-compose.yml) should have a different script in it considering I’m running caddy. Anyways, if you know what’s going on with this please let me know.

Again, thank you for taking the time the help me out.

Tom thank you so much man,

I just fixed it. All I did was keep my existing docker-compose.yml file and just switch “image:n8nio/n8n:ai-beta” to “build: .” and now ffmpeg is active in there.

Again, thank you so much for all your help, you are a true legend my friend!

2 Likes

Amazing, glad to hear this works for you!

Hey, sorry to revive the thread.

I was looking to do exactly the same. But, I am getting stuck as I am not sure how to execute the commands through the node.

For context, I have built the Docker image using the “latest” image from n8n and added the lines you gave.

When I execute ffmpeg --help, I get a similar response which shows I have installed it correctly. But, I am getting a permissions error whenever I run the command.

I am also using Digital Ocean to host the instance.

Hi @Jayavel, this is not an n8n issue but an error thrown by ffmpeg. Make sure you allow your n8n user to write to your /files folder.

If you’re using n8n to run docker also keep in mind that your docker container will not typically have access to the full host filesystem and you’d need to specifically mount your local directory before being able to access it.

1 Like

Jumping on the bandwagon here; I expect more people will come to this exact thread to do the exact same seeing as how powerful the whisper API is and associated use cases.

What is the best way forward for us using the cloud version of N8N?

I am right now setting up my own custom service on render.com to handle the splitting but ultimately would prefer not to have to do too much custom code and services.

Any better option available in your opinion?

Thanks

Hi @revhuntr, I doubt n8n cloud will get support for executing arbitrary binaries like ffmpeg through the Execute Command node anytime soon, mostly for security reasons.

You could find a suitable REST API handling necessary conversions, but for the time being self-hosting n8n is the only way to allow this.

I set up a web sevice that does the deed - in case anyone lands here and needs it too just ping me

1 Like

Thanks man. Since I had recreated the entire setup with the custom image, I forgot to run the permissions command for the /files folder. Now it is working.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.