The Computer Oracle

Is it possible to speed up video with audio using ffmpeg, without changing audio pitch?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: The Builders

--

Chapters
00:00 Is It Possible To Speed Up Video With Audio Using Ffmpeg, Without Changing Audio Pitch?
00:53 Accepted Answer Score 45
01:26 Answer 2 Score 10
02:29 Answer 3 Score 2
02:42 Thank you

--

Full question
https://superuser.com/questions/1324525/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#audio #video #ffmpeg

#avk47



ACCEPTED ANSWER

Score 45


It can be done with ffmpgeg using a complex filter:

  ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv

Documentation: https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video

The command above works if you want to multiply by 2 the speed. If you want to multiply by any <x>, the parameters become:

  ffmpeg -i input.mkv -filter_complex "[0:v]setpts=<1/x>*PTS[v];[0:a]atempo=<x>[a]" -map "[v]" -map "[a]" output.mkv

For instance, if you want to multiply by 1.15, the command is:

  ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.87*PTS[v];[0:a]atempo=1.15[a]" -map "[v]" -map "[a]" output.mkv



ANSWER 2

Score 10


You can speed up video by 2.5X for example by doing this:

ffmpeg -i "input_video.mp4" -vf "setpts=(PTS-STARTPTS)/2.5" -af atempo=2.5 "input_video_2.5X.mp4"

Here is a bash script you can use for convenience (say convert_video.sh):

#!/bin/bash

# Arguments
FILE_RAW=$1
SPEED_FACTOR=${2:-1.0} # Default is 1.0 X speed

# Prepare variables
BASE_PATH=$(dirname $(readlink -f $FILE_RAW))
FILENAME_EXT="$(basename "${FILE_RAW}")"
FILENAME_ONLY="${FILENAME_EXT%.*}"
EXT_ONLY="${FILENAME_EXT#*.}"
FILENAME_ONLY_PATH="${BASE_PATH}/${FILENAME_ONLY}"

# Speed up/slow down video
ffmpeg -i "${FILENAME_ONLY_PATH}.${EXT_ONLY}" -vf "setpts=(PTS-STARTPTS)/${SPEED_FACTOR}" -af atempo=$SPEED_FACTOR "${FILENAME_ONLY_PATH}_${SPEED_FACTOR}X.${EXT_ONLY}"

Note: Make script executable: chmod +x convert_video.sh

Usage (Output File: <PATH_TO_INPUT_VIDEO>_<OPTIONAL_SPEED>X.mp4)

. <PATH_TO_THIS_SCRIPT>/convert_video.sh <PATH_TO_INPUT_VIDEO> <OPTIONAL_SPEED>

Example 1: Speed up video to 2.5X (Output: ~/Videos/input_video_2.5X.mp4)

. ~/convert_video.sh ~/Videos/input_video.mp4 2.5

Example 2: Slow down video to 0.5X (Output: ~/Videos/input_video_0.5X.mp4)

. ~/convert_video.sh ~/Videos/input_video.mp4 0.5

Hope it helps.




ANSWER 3

Score 2


The code I used.

speed_change(){
    input=$1
    output=$2
    a_speed=$3
    v_speed=$(awk -v a_speed=$a_speed 'BEGIN{print 1/a_speed}')
    echo -e "\nSpeed up ${a_speed}X, para to atempo is ${a_speed}, para to setpts is ${v_speed}.\n"
    ffmpeg -i $input -c:v hevc_videotoolbox -vf "setpts=$v_speed*PTS" -af atempo=$a_speed $output
}