Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FFMPEG
#1
I'm making a program in python that takes an MP4 video, extracts the audio and compresses it, and then takes that compressed audio and takes 0.1 second slices and will process them when I get to coding that, but for now, I'm just making the system for getting the slices. But for some reason, the to part of this defaults to the end of the audio instead of where it's meant to go.

ffmpeg.input("temp.mp3", ss=audioTime).output("slice.mp3", to=audioTime+0.1, acodec="copy").run(quiet=True, overwrite_output=True)
Whole Script:
import moviepy.editor as mp
import ffmpeg
import os

def main():
    wordList = []

    running = True

    percentage = 0

    print("Type ath to video:")

    path = input()

    video = mp.VideoFileClip(path)

    duration = video.duration

    audioTime = 0

    audio = video.audio

    audio.write_audiofile("tempUncompressed.mp3")

    probeBitrate = ffmpeg.probe("tempUncompressed.mp3", v='error', select_streams='a', show_entries='stream=bit_rate')
    probeSampleRate = ffmpeg.probe("tempUncompressed.mp3", v='error', select_streams='a', show_entries='stream=sample_rate')

    bitrate = probeBitrate["streams"][0]['bit_rate']

    sampleRate = probeSampleRate["streams"][0]["sample_rate"]

    bitrate = int(bitrate)

    sampleRate = int(sampleRate)

    bitrate /= 1000

    if bitrate < 100:
        print("The video's audio bitrate is too low! Aborted.")

        running = False

    if sampleRate < 32000:
        print("The video's audio sample rate is too low! Aborted.")

        running = False

    ffmpeg.input("tempUncompressed.mp3").output("temp.mp3", audio_bitrate="100k", ar=32000, y=None).run(quiet=True)

    os.remove("tempUncompressed.mp3")

    duration = round(duration)

    while running:
        ffmpeg.input("temp.mp3", ss=audioTime).output("slice.mp3", to=audioTime+0.1, acodec="copy").run(quiet=True, overwrite_output=True)

        audioTime += 1

        percentage = audioTime/duration*100

        print(f"\rCreating video... {percentage:.2f}%   ", end="", flush=True)

        if audioTime >= duration:
            print("\nDone!")

            running = False

main()
Output:

leon@chickenwing:~/Documents/Ambitious Project$ python3 main.py
Type path to video:
video.mp4
MoviePy - Writing audio in tempUncompressed.mp3
MoviePy - Done.
Creating video... 100.00%
Done!
leon@chickenwing:~/Documents/Ambitious Project$
Gribouillis write Feb-12-2025, 06:06 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 959 Nov-21-2024, 11:48 PM
Last Post: haihal
  How to 'soft-embedd' subtitles with python-ffmpeg Pavel_47 2 3,657 Jul-04-2022, 12:33 PM
Last Post: Pavel_47
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 4,308 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  MovieWriter ffmpeg unavailable; using Pillow instead. Joni_Engr 1 39,637 Aug-13-2021, 03:39 PM
Last Post: deanhystad
  When piping a FFMPEG stream to PyAudio, I get a "click" on every loop klehman 0 6,109 Dec-15-2019, 04:22 AM
Last Post: klehman
  how do you use stupid ffmpeg ineedastupidusername 0 5,063 Nov-10-2017, 05:38 AM
Last Post: ineedastupidusername

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020