2024-08-19

From Cassette Tapes to YouTube: A Journey into Digital Preservation

 In an era where digital media reigns supreme, converting old cassette tapes into YouTube videos might seem like an odd endeavor. Yet, for enthusiasts of vintage audio, it’s a meaningful way to preserve and share cherished recordings. This process combines nostalgia with modern technology, offering a bridge between the past and the present.

Why go through the trouble of converting cassette tapes into video files? The answer lies in preservation and accessibility. Cassette tapes, once a popular medium for recording music and personal messages, are prone to physical wear and tear. Digital formats, however, offer a more stable and enduring method of preservation. By converting these recordings into video files, you not only safeguard them against deterioration but also make them available on platforms like YouTube, where they can reach a global audience.

The technical side of this transformation involves a blend of scripting and multimedia tools. The process begins with a straightforward script, which might look deceptively simple but performs a series of intricate tasks. The script prompts the user to input the necessary details: whether to use the last 10 seconds of the audio or the entire file, the paths for the image and audio files, and the desired output file name.

Next comes the crucial step of image processing. Before creating the video, the script uses FFprobe to check the dimensions of the image. Video encoders often require that dimensions be divisible by 2 for optimal performance. If the image doesn’t meet this criterion, the script employs FFmpeg to crop it slightly, ensuring it’s ready for video encoding.

The final act is the actual creation of the video. Depending on the user’s choice, the script tells FFmpeg to either use the last 10 seconds of the audio or the full file. The image is set to loop throughout the video, creating a visual backdrop for the audio. With commands that adjust frame rates and video duration, the script ensures the final product aligns perfectly with the audio content.

This blend of old and new—vintage audio paired with contemporary digital formats—makes for an intriguing process. It’s a nod to the past, offering a modern twist on how we archive and share our histories. Whether you’re an audiophile, a history buff, or simply someone looking to preserve personal memories, this method provides a practical solution for turning analog treasures into digital keepsakes. As technology continues to evolve, it’s reassuring to know that with a bit of scripting and the right tools, we can keep our past alive in the ever-expanding digital world.

Appendix. The Enhanced Script: Key Features and Functionality

The provided script offers an improved approach to converting audio and image files into video. It addresses some additional aspects of image processing, particularly focusing on ensuring that both the width and height of the image are compatible with video encoding standards.

Script Breakdown

Initial Setup

@echo off
setlocal enabledelayedexpansion

The script begins by disabling command echoing with @echo off and enabling delayed variable expansion with setlocal enabledelayedexpansion. This setup is essential for managing variables dynamically within the script.

User Input

:: Prompt user for the key (t for last 10 seconds, a for full MP3)
echo Enter the key (t for last 10 seconds, a for full MP3):
set /p key=

:: Debugging output
echo Key entered: "%key%"

:: Prompt user for the image file path
echo Enter the path to the image file:
set /p image_file=

:: Prompt user for the audio file path
echo Enter the path to the audio file:
set /p audio_file=

:: Prompt user for the output video file name
echo Enter the output video file name:
set /p output_file=

The script prompts the user for necessary input:

  • Key: Determines whether to use the last 10 seconds of audio (t) or the entire audio file (a).
  • Image file path: Location of the image to be used in the video.
  • Audio file path: Location of the audio file.
  • Output video file name: Desired name for the resulting video.

File Existence Check

:: Check if the image file exists
if not exist "%image_file%" (
    echo The image file does not exist.
    exit /b 1
)

:: Check if the audio file exists
if not exist "%audio_file%" (
    echo The audio file does not exist.
    exit /b 1
)

The script verifies that both the image and audio files exist. If either file is missing, it prints an error message and exits.

Image Dimensions Verification

Getting Dimensions
:: Get the image width and height using ffprobe and store them in separate temporary files
ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "%image_file%" > width.txt
ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "%image_file%" > height.txt

The script uses ffprobe to retrieve the width and height of the image, saving these values in separate temporary files (width.txt and height.txt). The dimensions are then read from these files and the temporary files are deleted.

Why Even Dimensions Matter

Video codecs, such as H.264 used by ffmpeg, often require that the width and height of the image be divisible by 2. This requirement ensures efficient encoding and decoding, as many video processing techniques, like chroma subsampling, depend on even dimensions. Images with odd dimensions can lead to complications in video processing and playback issues.

Checking and Adjusting Image Dimensions
:: Check if ffprobe was successful
if "%width%"=="" (
    echo Failed to get the image width.
    pause
    exit /b 1
)

if "%height%"=="" (
    echo Failed to get the image height.
    pause
    exit /b 1
)

:: Display the width and height
echo Width: %width%
echo Height: %height%

:: Check if width is divisible by 2
set /a width_result=width %% 2

:: Check if height is divisible by 2
set /a height_result=height %% 2

:: Initialize cropping flag
set crop_needed=0

if !width_result! neq 0 (
    echo Width is not divisible by 2
    set /a crop_needed=1
)

if !height_result! neq 0 (
    echo Height is not divisible by 2
    set /a crop_needed=1
)

The script checks if the width and height values were successfully retrieved. It then verifies if these dimensions are divisible by 2. If either dimension is not divisible by 2, the script sets a flag to indicate that cropping is needed.

Cropping the Image
:: Check if cropping is needed
if !crop_needed! neq 0 (
    echo Cropping 1 pixel from the width or height to make it divisible by 2...

    :: Extract only the filename and extension from the input file
    for %%f in ("%image_file%") do (
        set "filename=%%~nf"
        set "extension=%%~xf"
        set "filepath=%%~dpf"
    )

    :: Define a temporary output file name in the same directory as the input file
    set "cropped_image=!filepath!!filename!_cropped!extension!"

    :: Display the file names for debugging
    echo Input file: "%image_file%"
    echo Output file: "!cropped_image!"

    :: Crop the image to remove 1 pixel if needed
    ffmpeg -i "%image_file%" -vf "crop=iw-mod(iw\,2):ih-mod(ih\,2)" "!cropped_image!"

    :: Check if cropping was successful
    if exist "!cropped_image!" (
        echo Image cropped successfully.
        echo Overwriting the original image with the cropped image.
        move /y "!cropped_image!" "%image_file%"
    ) else (
        echo Failed to crop the image. Check the command and file formats.
        pause
        exit /b 1
    )
)

If cropping is needed, the script generates a temporary file name for the cropped image. It uses ffmpeg with the crop filter to adjust the dimensions to be divisible by 2. The command -vf "crop=iw-mod(iw\,2):ih-mod(ih\,2)" adjusts the width and height if necessary. After cropping, it checks if the new image file exists and replaces the original image with the cropped version if successful.

Video Creation Based on User Input

:: Debugging output
echo Input image file: "%image_file%"
echo Audio file: "%audio_file%"
echo Output video file: "%output_file%"

:: Process based on the key
if /i "%key%"=="t" (
    echo Key is 't'
    echo Processing with last 10 seconds of audio...
    ffmpeg -loop 1 -i "%image_file%" -i "%audio_file%" -c:v libx264 -tune stillimage -preset ultrafast -b:v 500k -c:a copy -shortest -r 1 -t 10 "%output_file%"

) else if /i "%key%"=="a" (
    echo Key is 'a'
    echo Processing with full audio...
    ffmpeg -loop 1 -i "%image_file%" -i "%audio_file%" -c:v libx264 -tune stillimage -preset ultrafast -b:v 500k -c:a copy -shortest -r 1 "%output_file%"

) else (
    echo Invalid key. Please enter 't' for last 10 seconds or 'a' for full MP3.
    exit /b 1
)

Based on the user’s input key, the script uses ffmpeg to generate the video:

  • Key t: Uses the last 10 seconds of the audio file.
  • Key a: Uses the entire audio file.

The ffmpeg command parameters:

  • -loop 1: Loops the image throughout the video.
  • -i "%image_file%": Input image file.
  • -i "%audio_file%": Input audio file.
  • -c:v libx264: Video codec.
  • -tune stillimage: Optimization for still images.
  • -preset ultrafast: Fast encoding with reduced compression efficiency.
  • -b:v 500k: Video bitrate.
  • -c:a copy: Copies the audio stream.
  • -shortest: Matches the video duration to the shortest input.
  • -r 1: Sets frame rate to 1 fps.

Final Steps

endlocal
pause

The script concludes by restoring the previous environment settings with endlocal and keeping the console window open with pause for user review.

Get the full script in the github - https://github.com/didzislauva/cassete2video