Short videos split by beats using Python and FFMPEG

ljubomir38

New member

😎 Video​

🤔 Why?​


I'm scrolling through Instagram every day, and I'm like, can I do some cool videos or edits?

But I've never used any video editing software...

But I can code!

So here it goes...

📝 What?​

  1. Take an arbitrary amount of clips and one audio.
  2. Combine it in a short video where small clips are switching one after another when next beat comes in.

😤 Try it yourself​

  1. Clone code from GitHub Gist to some folder,
    Code:
    video_edits
    , for example.
  2. Install dependencies.
    1. Librosa python package for audio analysis to extract beats info.
    2. FFMPEG, I just downloaded binary for windows.
  3. Change
    Code:
    video_file_paths
    and
    Code:
    audio_file_path
    in
    Code:
    main.py
    to your video and audio files.
  4. Run
    Code:
    python ./video_edits/main.py
    .
  5. Open generated
    Code:
    video.mp4
    in any player.
  6. Enjoy!

💡 Main ideas​


🎵 With
Code:
Librosa
you can get beats from audio:

Code:
y, sr = librosa.load(audio_file_path)
beat_times = librosa.beat.beat_track(y=y, sr=sr, units="time")[1].tolist()

🎥 With FFMPEG you can crop and resize video with filter filter:

Code:
crop='
   if(
       gte(
           in_h,
           1920 * (in_w / 1080)
       ),
       in_w,
       1080 * (in_h / 1920)
   )
':'
   if(
       gte(
           in_h,
           1920 * (in_w / 1080)
       ),
       1920 * (in_w / 1080),
       in_h
   )
',
scale=w=1080:h=1920,
setsar=1

Crop looks like:
Code:
crop='width':'height'
.

Crop, as it's present in code, will crop starting from middle of the video, so central part.

Code:
in_h
and
Code:
in_w
are build-in FFMPEG variables for original video height and width.

Logic is next:
  1. If it's extremely high video, we want to preserve width and crop height.
  2. If it's extremely wide video, we want to preserve height and crop width.
  3. Otherwise we preserve height and crop width.
Code:
scale
will scale cropped video to desired dimensions.

Code:
setsar
sets the correct aspect ratio, otherwise the scaled and cropped output might look distorted, stretched.

🐍 Finally with Python we can bring it all together:
  1. Resize each video with FFMPEG and trim time to duration of a beat, which is duration from previous beat to current one.
  2. All videos then concatenated to one video clip.
  3. Audio part with selected beats is added to a resulting video.

🔗 Links​


Media sources HERE.

Librosa documentation: https://librosa.org/doc/0.10.1/.

FFMPEG guide: https://img.ly/blog/ultimate-guide-to-ffmpeg.

⏭️ What's next?​


I'll continue to share interesting edits and how I did it!

Maybe will create repo where I'll be keeping all utils for edits.

🤦‍♂️ OF COURSEEEEE​


Check my socials, if you want!
 

Similar threads

Back
Top